出于某种原因,多态的源类型 has_many :through
尽管设置了 :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
标记项目:
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。知道为什么吗?谷歌什么也没找到。
最佳答案
我自己想出了这个,只是回答,因为我确定我不是第一个或最后一个犯这个愚蠢错误的人(实际上我以前可能已经犯过)。我将列类型作为整数而不是字符串放在 taggable_type 字段上。
您会认为这可能会导致错误,但事实并非如此。它只是不起作用。
关于ruby-on-rails - 为什么这个多态关联的 source_type 总是 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22067860/