[英]Create a polymorphic STI association
Here's my self referential model and it's two join tables: 这是我的自我参照模型,它是两个联接表:
class Discourse < ActiveRecord::Base
belongs_to :forum
belongs_to :user
has_many :impressions
has_many :discourse_replies
has_many :replies, through: :discourse_replies
has_many :reply_retorts
has_many :retorts, through: :reply_retorts
end
class DiscourseReply < ActiveRecord::Base
belongs_to :discourse
belongs_to :reply, class_name: 'Discourse', foreign_key: 'reply_id'
end
class ReplyRetort < ActiveRecord::Base
belongs_to :reply
belongs_to :retort, class_name: 'Discourse', foreign_key: 'retort_id'
end
It seems to be working well...I can do this in the rails console: 似乎运行良好...我可以在rails控制台中执行此操作:
2.0.0p247 :044 > fd = Discourse.create(title: 'first', body: 'first')
=> #<Discourse id: 139, user_id: nil, title: "first", body: "first", deleted: nil, delete_date: nil, created_at: "2014-04-07 20:38:06", updated_at: "2014-04-07 20:38:06", forum_id: nil>
2.0.0p247 :046 > fdr = fd.replies.create(title: 'second relpy to first', body: 'second reply to first')
=> #<Discourse id: 141, user_id: nil, title: "second relpy to first", body: "second reply to first", deleted: nil, delete_date: nil, created_at: "2014-04-07 20:38:51", updated_at: "2014-04-07 20:38:51", forum_id: nil>
2.0.0p247 :047 > fdrr = fdr.retorts.create(title: 'a reply to a reply', body: 'a reply to a reply')
=> #<Discourse id: 142, user_id: nil, title: "a reply to a reply", body: "a reply to a reply", deleted: nil, delete_date: nil, created_at: "2014-04-07 20:39:27", updated_at: "2014-04-07 20:39:27", forum_id: nil>
2.0.0p247 :048 > fdrrr = fdrr.retorts.create(title: 'a reply to a reply to a reply', body: 'a reply to a reply reply')
=> #<Discourse id: 143, user_id: nil, title: "a reply to a reply to a reply", body: "a reply to a reply reply", deleted: nil, delete_date: nil, created_at: "2014-04-07 20:39:47", updated_at: "2014-04-07 20:39:47", forum_id: nil>
2.0.0p247 :050 > fdr.retorts
=> #<ActiveRecord::Associations::CollectionProxy [#<Discourse id: 142, user_id: nil, title: "a reply to a reply", body: "a reply to a reply", deleted: nil, delete_date: nil, created_at: "2014-04-07 20:39:27", updated_at: "2014-04-07 20:39:27", forum_id: nil>]>
However, I need to find out the parent association, but can't figure out how to do it: 但是,我需要找出父关联,但无法弄清楚该怎么做:
2.0.0p247 :053 > fdr.discourse # I want this to return the 'fd' instance
NoMethodError: undefined method `discourse` for #<Discourse:0x00000007080eb0>
2.0.0p247 :055 > fdrrr.reply # I want this to return the 'fdrr' instance
NoMethodError: undefined method `reply` for #<Discourse:0x000000070db860>
2.0.0p247 :055 > fdrrr.parent # I want this to return the 'fdrr' instance
NoMethodError: undefined method `parent' for #<Discourse:0x0000000672b428>
2.0.0p247 :055 > fdrrr.parent.try(:id) # I want this to return the 'fdrr' instance
NoMethodError: undefined method `parent' for #<Discourse:0x0000000672b428>
Nothing is working! 什么都没用! However, if I were to set up polymorphic association STI, it work, right?
但是,如果我要建立多态关联STI,它会起作用,对吗? As a nooby this is a bit difficult, especially on such a complex self-referential model, but the answer boils down to this:
作为nooby,这有点困难,尤其是在这种复杂的自引用模型上,但是答案归结为:
What columns to I need to add to my discourses table and How should I tweak my relationships? 我需要在“话语”表中添加哪些列 ?如何调整我的关系?
class Discourse < ActiveRecord::Base
belongs_to :forum
belongs_to :user
has_many :impressions
has_many :discourse_replies
has_many :replies, through: :discourse_replies
has_many :reply_retorts
has_many :retorts, through: :reply_retorts
end
I does not find self-referencing relationship in model 'Discourse'. 我在模型“话语”中没有发现自引用关系。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.