首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建多态STI关联

创建多态STI关联
EN

Stack Overflow用户
提问于 2014-04-08 12:08:12
回答 1查看 60关注 0票数 0

这是我的自引用模型,它是两个连接表:

代码语言:javascript
复制
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

它似乎运行得很好.我可以在rails控制台上这样做:

代码语言:javascript
复制
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>]> 

但是,我需要找出父关联,但不知道如何实现:

代码语言:javascript
复制
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,这有点困难,特别是在这样一个复杂的自引用模型上,但答案归结为:

我需要添加哪些列到我的语篇表如何调整我的关系?

EN

回答 1

Stack Overflow用户

发布于 2014-04-08 12:28:12

代码语言:javascript
复制
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

我在“话语”模型中找不到自我参照关系。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22936560

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档