I am developing a rails app where users can post, must like facebook. I want to implement a notification systems that alerts users to new posts. However, I am having trouble on how to tell if a user has viewed posts or not. I am literally clueless.
I am using devise gem which gives me access to certain user stats (if this helps):
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :default => "", :null => false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", :default => 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
t.integer "failed_attempts", :default => 0
t.string "unlock_token"
t.datetime "locked_at"
t.string "authentication_token"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "username", :default => "", :null => false
t.integer "admin", :default => 0
end
And my post model:
create_table "posts", :force => true do |t|
t.integer "user_id"
t.text "content"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
How can I implement a system that knows if a user has seen a post or not?
simple aproach would be like that:
create a model called Seen
rails g model Seen post:references user:references
models/seen.rb
belongs_to :user
belongs_to :post
models/user.rb
has_many :seens
has_many :seen_posts, through: :seens, source: :post
models/post.rb
has_many :seens
has_many :seen_users, through: :seens, source: :user
and you can create a method something like that
models/post.rb
def seen_by?(user)
seen_user_ids.include?(user.id)
end
controllers/posts_controller.rb
def show
#post = Post.find(params[:id])
current_user.seen_posts << #post unless #post.seen_by?(current_user)
end
Related
I'm trying to create an app that accepts an SMS message through Twilio, and then creates a check-in/out transaction that is tied to both employee models and item models. A simple SMS-based item checkout/checkin tracker. I have the twilio app wired up to listen on tooler.herokuapp.com/twilio/twilio_create, but when I send messages to the number, nothing happens and I get a 404 error within twilio's logs. Not sure exactly what's going on, was hoping someone might be able to help. In this case, I'm taking the FROM from twilio and putting it into employee_id, and the BODY from twilio and putting it into item_id. Why won't it create new transactions?
db/schema.rb
ActiveRecord::Schema.define(:version => 20130516162824) do
create_table "employees", :force => true do |t|
t.string "phone"
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "items", :force => true do |t|
t.string "description"
t.string "assettag"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "transactions", :force => true do |t|
t.boolean "status"
t.integer "item_id"
t.integer "employee_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "transactions", ["employee_id"], :name => "index_transactions_on_employee_id"
add_index "transactions", ["item_id"], :name => "index_transactions_on_item_id"
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :default => "", :null => false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", :default => 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
end
app/controllers/twilio_controller.rb
class TwilioController < ApplicationController
def process_sms
#city = params[:FromCity].capitalize
#state = params[:FromState]
render 'process_sms.xml.erb', :content_type => 'text/xml'
end
def twilio_create
#transaction = Transaction.new(:item_id => params[:Body], :employee_id => params[:From])
#transaction.save
end
end
app/views/twilio/twilio_create.xml.erb
<Response>
<Sms>Received. You checked out <%= #body %>, <%= #from %> you lucky bastard.</Sms>
</Response>
I already got it working with the process_sms page, so I know that it's something with the twilio_create function. What am I doing wrong?
Should the URL be tooler.herokuapp.com/twilio/twilio_create.xml? You can check rake routes to see all URLs that conforms to your config/routes.rb.
Actually, Rails already has CRUD convention. Since you are creating a twilio resource, your config/routes.rb should be:
# config/routes.rb
resources :twilio do
collection do
get :process_sms
end
end
In the controller, you should use def create instead of def twilio_create.
class TwilioController < ApplicationController
def process_sms
#city = params[:FromCity].capitalize
#state = params[:FromState]
render 'process_sms.xml.erb', :content_type => 'text/xml'
end
def create
#transaction = Transaction.new(:item_id => params[:Body], :employee_id => params[:From])
#transaction.save
end
end
Lastly, rename app/views/twilio/twilio_create.xml.erb to app/views/twilio/create.xml.erb.
In order to create a new transaction, do a post request to tooler.herokuapp.com/twilio.xml. That URL will hit the def create in TwilioController and render app/views/twilio/create.xml.erb.
If it still doesn't work because of 404 error, you can check rake routes to see all URLs that conforms to your config/routes.rb.
Setting up the database, I am just curious if I did it correctly, as it looks a bit off. There are people, who have a user account, and a role (teacher or student). they are participants in a class (where a class has many students and teachers; a student has many classes; a teacher has many classes). I think my class_instruction model is off in the DB, but please tell me if it will work, or if there is a better way (like maybe with a has_many_through table of participants)
schema.rb:
ActiveRecord::Schema.define(:version => 20130524160107) do
create_table "class_instructions", :force => true do |t|
t.string "name"
t.datetime "time"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "person_id"
end
add_index "class_instructions", ["person_id"], :name => "index_class_instructions_on_person_id"
create_table "people", :force => true do |t|
t.string "firstName"
t.string "lastName"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "people", ["user_id"], :name => "index_people_on_user_id"
create_table "roles", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "user_roles", :force => true do |t|
t.integer "user_id"
t.integer "role_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "user_roles", ["role_id"], :name => "index_user_roles_on_role_id"
add_index "user_roles", ["user_id"], :name => "index_user_roles_on_user_id"
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :default => "", :null => false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", :default => 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
end
My concern is that the person_id is part of the class. Is this correct?
ClassInsturction.rb :
class ClassInstruction < ActiveRecord::Base
belongs_to :people
has_many :cassignments
has_many :assignments, :through => :cassignments
def className
self.name
end
def classAssignments
return self.cassignments
end
end
I would make a joining table that has both class and people and use has many through. If people have many classes and classes have many people you can not do this any other way.
I have seen usually belongs_to with singular form:
belongs_to :person
I am not sure if this is what you are asking for though.
Im setting up active admin on my rails app. I ran the generator to create the user resource but when i click on the users link on the admin dashboard i get:
NoMethodError in Admin/users#index
Showing /Users/nelsonkeating/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/gems/1.9.1/gems/activeadmin-0.4.4/app/views/active_admin/resource/index.html.arb where line #1 raised:
undefined method `city_id_contains' for #<MetaSearch::Searches::User:0x007fde92d69840>
Extracted source (around line #1):
1: render renderer_for(:index)
I have no clue what is generating this or where the error is coming from.. Any ideas? Thanks! (please let me know if you need to see any other files)
Models:
user.rb
class User < ActiveRecord::Base
rolify
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :province_id, :city_id
belongs_to :province
belongs_to :city
province.rb
class Province < ActiveRecord::Base
has_many :cities
has_many :users
end
city.rb
class City < ActiveRecord::Base
belongs_to :province
has_many :users
end
schema.rb
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :default => "", :null => false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", :default => 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "name"
t.date "date_of_birth"
t.string "address"
t.string "gender"
t.integer "zipcode"
t.string "city"
t.string "status"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
t.integer "province_id"
t.integer "city_id"
end
create_table "provinces", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "cities", :force => true do |t|
t.string "name"
t.integer "province_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
This question helped me solve my problem. I had to read the comments to figure out how to solve it so I am providing the answer here:
If you have a model that contains a belongs_to relationship, Active Admin will not like it if you have a column name that matches your belongs to foreign key id column name.
For instance in this case your foreign key is 'city_id' but you also have a column named 'city'. Active Admin doesn't like this. And likewise it really doesn't make sense to have that column to begin with. Since you will access the city through the relationship.
To fix this you should create a migration that removes the city column.
class RemoveCityFromUser < ActiveRecord::Migration
def up
remove_column :users, :city
end
end
I have the following models:
class Constraint < ActiveRecord::Base
belongs_to :constraint_category
end
class ConstraintCategory < ActiveRecord::Base
has_many :constraints
end
The models have these attributes (from db/schema.rb):
create_table "constraint_categories", :force => true do |t|
t.string "value"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.boolean "active"
end
create_table "constraints", :force => true do |t|
t.string "phrase"
t.integer "constraint_category_id", :limit => 255
t.boolean "active"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
I would like to create a query that finds all constraints where the "active" attribute is "true" and the "constraint_category.value" is "Noun".
Would love any advice on getting there.
Constraint.joins(:constraint_category).where('constraints.active = ? and constraint_categories.value = ?', true, 'Noun')
See conditions and joins in the guide.
I've created a one-to-one association between my Admin and Report models and it isn't working just yet. I'm using Devise to log in via an Admin model so in the controller I'm using the current_admin helper. Silly question, but what migration do I need to run to get this working?
Error
ActiveRecord::StatementInvalid (PGError: ERROR: column reports.admin_id does not exist
2011-10-14T09:16:57+00:00 app[web.1]: LINE 1: SELECT "reports".* FROM "reports" WHERE ("reports".admin_id = ...
Report model
belongs_to :admin, :foreign_key => "admin_id"
Admin model
has_one :report, :foreign_key => "admin_id"
Controller
#report = current_admin.report
Schema
create_table "reports", :force => true do |t|
t.string "name"
t.string "description"
t.datetime "created_at"
t.datetime "updated_at"
t.string "user_id"
end
create_table "admins", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :limit => 128, :default => "", :null => false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", :default => 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
end
You need admin_id in reports. You can remove the foreign_key stuff in your models. That's done automatically. Create a migration, add
add_column :reports, :admin_id, :integer
Run rake db:migrate and you're done.