Rails 中的多态模型问题 - 它是否为您维护关系?
假设您有一个 SiteUpdate 和一个 Comment 模型,并且您希望使 Comment 具有多态性。您使评论包含“commentable_id”和“commentable_type”...
这是我们的评论模型:
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
validates_presence_of :content
validates_presence_of :commentable
end
这是我们的SiteUpdate:
class SiteUpdate < ActiveRecord::Base
belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
has_many :comments, :as => :commentable
validates_presence_of :subject
validates_length_of :subject, :maximum => 80
validates_presence_of :intro
validates_length_of :intro, :maximum => 200
validates_presence_of :text
validates_presence_of :author
scope :sorted, order("site_updates.created_at desc")
end
Rails 是否将commentable 链接到site_update 实例,还是我必须手动执行此操作?
@site_update.comments << Factory.build(:comment, :commentable_id => nil)
@site_update.save
这失败了->它抱怨 comment.commentable_id 不应该为空(我在 Comment 模型中设置了此验证)。
那么我是手动执行此操作,还是设置不正确?
或者我根本就没有验证它?
Let's say you have a SiteUpdate and a Comment model, and you want to make Comment polymorphic. You make comment hold a "commentable_id" and "commentable_type"...
Here's our comment model:
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
validates_presence_of :content
validates_presence_of :commentable
end
Here is our SiteUpdate:
class SiteUpdate < ActiveRecord::Base
belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
has_many :comments, :as => :commentable
validates_presence_of :subject
validates_length_of :subject, :maximum => 80
validates_presence_of :intro
validates_length_of :intro, :maximum => 200
validates_presence_of :text
validates_presence_of :author
scope :sorted, order("site_updates.created_at desc")
end
Does Rails link the commentable to the site_update instance, or do I have to do that manually?
@site_update.comments << Factory.build(:comment, :commentable_id => nil)
@site_update.save
This fails -> it complains that the comment.commentable_id should not be blank (I set this validation in the Comment model).
So do I do this manually, or do I set this up incorrectly?
Or do I just not validate it at all?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您的
@site_update
对象是一个新对象。如果是这样......Rails 协会有一件有点烦人的事情。在保存记录之前您无法真正添加它们。
发生的情况是,您有一个没有 ID 的新站点更新对象。您为该站点更新构建了一个新的评论对象,因此它将 commentable_type 设置为“SiteUpdate”,但是,还没有 ID,因此它将 commentable_id 设置为 nil。您保存后,它会冒泡以保存关联的对象,但它不会将评论 commentable_id 设置为 SiteUpdate ID,因为它不存在。
因此,如果你将其更改为:
它应该可以工作。
如果它不是新记录...它应该按原样工作。
I'm making an assumption that your
@site_update
object is a new object. If so...There's a somewhat annoying thing about rails associations. You can't really add them before the record is saved.
What's happening is, you have a new site update object without an ID. You build a new comment object for that site update, so it sets the commentable_type to "SiteUpdate", however, there's no ID yet, so it sets the commentable_id to nil. You save, and it bubbles out to save associated objects, but it doesn't set the comment commentable_id to the SiteUpdate ID, because it doesn't exist.
So if you change it around to be :
it should work.
If it's not a new record...it should work as is.