简体   繁体   English

创建多态STI关联

[英]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.

相关问题 两个STI /多态之间的关联 - Association between two STI/Polymorphic STI&多态关联和user_id - STI & Polymorphic association & user_id Rails:构建涉及多态关联和 STI 的查询 - Rails: Structuring a query involving a polymorphic association and STI Rails:STI结构中的子类需要多态关联吗? - Rails: Polymorphic association needed for subclasses in STI structure? 通过与STI的多态关联“has_many:through”关联 - “has_many :through” association through a polymorphic association with STI 两个STI层次结构和它们之间的多态关联 - two STI hierarchies and polymorphic association(s) between them 无法让STI充当模型上的多态关联 - Can't get STI to act as polymorphic association on model 如果多态关联的类型列不指向 STI 的基本模型,为什么多态关联对 STI 不起作用? - Why polymorphic association doesn't work for STI if type column of the polymorphic association doesn't point to the base model of STI? 与`多态关联不支持计算类的关联问题。`在多态的belongs_to与sti - Association issues with `Polymorphic association does not support to compute class.` in polymorphic belongs_to with sti 在多态关联中创建动作不起作用 - Create action in polymorphic association not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM