I was hoping someone could explain to me why i can't save new record?
class User < ActiveRecord::Base
has_many :viewed_posts, :dependent => :destroy
has_many :posts, :through => :viewed_posts
def update_viewed_posts(post)
if viewed_posts.where("id IN(?)", post.id).blank?
puts '----------------------- NEVER READED POST'
viewed_posts.build(:question_id => post.id, :user_id=> self.id)
end
end
end
class ViewedPosts < ActiveRecord::Base
belongs_to :post
belongs_to :user
end
My question is when i call update_view_posts
method record is never save? Why?
update_view_posts
on show action.
class PostsController < Application开发者_JAVA技巧Controller
respond_to :html
before_filter :find_post, :only => [:show, :edit, :update, :destroy]
def show
if current_user
current_user.update_viewed_posts(@post)
end
respond_with(@post)
end
end
Maybe you want:
belongs_to :post
instead of
belongs_to :posts
精彩评论