Rails 3:has_many:直通保存问题?
我希望有人能向我解释为什么我无法保存新记录?
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
我的问题是当我调用 update_view_posts 方法时记录永远不会保存?为什么?
已编辑:添加了我的控制器。我在显示操作时调用 update_view_posts
。
class PostsController < ApplicationController
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
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 < ApplicationController
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许你想要:
而不是
Maybe you want:
instead of