繁体   English   中英

Has_one:通过多态的belongs_to

[英]Has_one :through polymorphic belongs_to

我有一个标记模型,具有与标记和可标记项的多态关联。 可标记项目都与feed_item(has_one:feed_item)关联。 我想将标记与feed_item相关联,即在tagging.rb中

   belongs_to :taggable, :polymorphic => true
   belongs_to :tag, :polymorphic => true

   has_one :feed_item, :through => :taggable

这个has_one关联不起作用。 例如,在控制台Tagging.first.feed_item中导致错误

 NameError: uninitialized constant Tagging::Taggable

有关建立这种关联的建议吗?

对于背景,以下是可标记对象模型的示例:

 has_one :feed_item, :as => :item

IE:标记has_one可标记对象has_one feed_item。

替换这个:

has_one :feed_item, :through => :taggable

有了这个:

def feed_item
  self.taggable.feed_item
end

现在你可以调用@taggable.feed_item并且它会工作。

你能看看https://github.com/rahul/SO_9288016并告诉我它是否适合你。

我还添加了一个小的db / development.sqlite3

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM