2014-04-08 89 views
0

這是我自己參考模型和它的兩個連接表:創建一個多態STI協會

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 

這似乎運作良好...我可以在鐵軌控制檯做到這一點:

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>]> 

不過,我需要找出家長協會,但無法弄清楚如何做到這一點:

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> 

沒有什麼工作!但是,如果我要建立多形性關聯性STI,那就行了,對吧?作爲nooby這是一個有點困難,尤其是在這樣一個複雜的自我指涉的模型,但答案歸結爲:

什麼列,我需要添加到我的話語表應該如何我調整了我的關係?

+0

你的回覆模式是什麼? –

+0

我沒有回覆模式。這可能是問題嗎? – user3067865

+0

那麼'reply_retort中的'belongs_to:reply'將不起作用 –

回答

0
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 

我在模型'話語'中找不到自我參照關係。