由於某些原因,多態性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