开发者

Rails 3: has_many :throught save problem?

开发者 https://www.devze.com 2023-01-31 00:42 出处:网络
I was hoping someone could explain to me why i can\'t save new record? class User < ActiveRecord::Base

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?

EDITED: Added my controller. I'm calling 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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号