【发布时间】:2014-03-30 20:14:54
【问题描述】:
由于某种原因,尽管设置了 :source_type,但多态 has_many :through 关联的源类型始终为 0。
这是我的模型的样子...
富:
has_many :tagged_items, :as => :taggable
has_many :tags, :through => :tagged_items
酒吧:
has_many :tagged_items, :as => :taggable
has_many :tags, :through => :tagged_items
标记项目:
belongs_to :tag
belongs_to :taggable, :polymorphic => true
标签:
has_many :tagged_items
has_many :foos, :through => :tagged_items, :source => :taggable, :source_type => "Foo"
has_many :bars, :through => :tagged_items, :source => :taggable, :source_type => "Bar"
据我所知,这是一个非常好的设置,我可以创建/添加标签,但 taggable_type 最终总是为 0。
知道为什么吗? Google 一无所获。
【问题讨论】:
-
我创建了一个带有测试 here 的 rails 4 示例。检查tagged_item_test.rb。我的测试通过了。那些测试对你来说应该失败了?
-
是的,失败了,但我发现了我的问题。基本上我是个白痴。我把 taggable_type 字段作为一个整数。哇!
-
hobberwickey 如果您认为我的示例项目帮助您解决了您的问题,请支持我的回答:)
标签: ruby-on-rails ruby-on-rails-4 has-many-through polymorphic-associations