[英]Has_one :through polymorphic belongs_to
I have a Tagging model, with polymorphic associations to tags and taggable items. 我有一个标记模型,具有与标记和可标记项的多态关联。 Taggable items all have an association to a feed_item (has_one :feed_item).
可标记项目都与feed_item(has_one:feed_item)关联。 I want to associate the tagging to the feed_item ie in tagging.rb
我想将标记与feed_item相关联,即在tagging.rb中
belongs_to :taggable, :polymorphic => true
belongs_to :tag, :polymorphic => true
has_one :feed_item, :through => :taggable
This has_one association doesn't work. 这个has_one关联不起作用。 For example in console Tagging.first.feed_item leads to the error
例如,在控制台Tagging.first.feed_item中导致错误
NameError: uninitialized constant Tagging::Taggable
Any advice to how one sets up this sort of association? 有关建立这种关联的建议吗?
For background, the following is an example of a taggable object's model: 对于背景,以下是可标记对象模型的示例:
has_one :feed_item, :as => :item
IE: A tagging has_one taggable object which has_one feed_item. IE:标记has_one可标记对象has_one feed_item。
Replace this: 替换这个:
has_one :feed_item, :through => :taggable
with this: 有了这个:
def feed_item
self.taggable.feed_item
end
Now you can call @taggable.feed_item
and it'll work. 现在你可以调用
@taggable.feed_item
并且它会工作。
Can you take a look at https://github.com/rahul/SO_9288016 and let me know if it does not work for you. 你能看看https://github.com/rahul/SO_9288016并告诉我它是否适合你。
I also added a tiny db/development.sqlite3 我还添加了一个小的db / development.sqlite3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.