gpt4 book ai didi

ruby-on-rails - Has_one :through polymorphic belongs_to

转载 作者:行者123 更新时间:2023-12-04 06:17:32 33 4
gpt4 key购买 nike

我有一个标记模型,具有与标记和可标记项目的多态关联。可标记项目都与 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 可标签对象,它有一个 feed_item。

最佳答案

替换这个:

has_one :feed_item, :through => :taggable

有了这个:
def feed_item
self.taggable.feed_item
end

现在您可以调用 @taggable.feed_item它会起作用的。

关于ruby-on-rails - Has_one :through polymorphic belongs_to,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9288016/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com