由于某些原因,多态性has_many :through
关联的源类型始终为0,尽管设置了:source_type
。为什么这个多态关联的source_type总是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
TaggedItem:
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一无所获。
我创建了一个轨道与测试[这里](4例如HTTPS ://github.com/raviolicode/has_many_polymorphic_example)。检查[tagged_item_test.rb](https://github.com/raviolicode/has_many_polymorphic_example/blob/master/test/models/tagged_item_test.rb)。我的测试通过。那些测试应该会失败吗? – raviolicode
是的失败了,但我找出了我的问题。基本上我是个白痴。我把taggable_type字段作为整数。卫生署! – hobberwickey
hobberwickey请upvote我的答案,如果你认为我的示例项目帮助你解决你的问题:) – raviolicode