[英]Why is the source_type for this polymorphic association always 0?
For some reason the source type for a polymorphic has_many :through
association is always 0, despite having set a :source_type
. 由于某种原因,多态的has_many :through
关联的源类型始终为0,尽管设置了:source_type
。
Here's what my models look like... 这就是我的模特的样子......
Foo: 富:
has_many :tagged_items, :as => :taggable
has_many :tags, :through => :tagged_items
Bar: 酒吧:
has_many :tagged_items, :as => :taggable
has_many :tags, :through => :tagged_items
TaggedItem: TaggedItem:
belongs_to :tag
belongs_to :taggable, :polymorphic => true
Tag: 标签:
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"
As near as I can tell that's a totally fine setup, and I am able to create / add tags, but the taggable_type
always ends up being 0. 尽可能接近我可以说这是一个非常精细的设置,我能够创建/添加标签,但taggable_type
总是最终为0。
Any idea's why? 任何想法都是为什么? Google has turned up nothing. 谷歌一无所获。
A working example with tests for the models in the question is found here . 这里可以找到问题模型测试的工作示例。
The reason why it didn't work on the question is the migration db/migrate/[timestamp]_create_tagged_items
should be generated like this: 它不能解决问题的原因是迁移db/migrate/[timestamp]_create_tagged_items
应该像这样生成:
class CreateTaggedItems < ActiveRecord::Migration
def change
create_table :tagged_items do |t|
t.belongs_to :tag, index: true
t.references :taggable, polymorphic: true
t.timestamps
end
end
end
Note that t.references :taggable, polymorphic:true
will generate two columns on schema.rb
: 注意t.references :taggable, polymorphic:true
将在schema.rb
上生成两列:
t.integer "taggable_id"
t.string "taggable_type"
So, with the same models in the question and this migration, the following tests pass: 因此,对于问题和迁移中的相同模型,以下测试通过:
require 'test_helper'
class TaggedItemTest < ActiveSupport::TestCase
def setup
@tag = tags(:one)
end
test "TaggedItems have a taggable_type for Foo" do
foo = Foo.create(name: "my Foo")
@tag.foos << foo
assert TaggedItem.find(foo.id).taggable_type == "Foo"
end
test "TaggedItems have a taggable_type for Bar" do
bar = Bar.create(name: "my Bar")
@tag.bars << bar
assert TaggedItem.find(bar.id).taggable_type == "Bar"
end
end
Note: Rails 3 has an active issue about has_many :through
and polymorphic associations, as shown here . 注:Rails 3中有有关活动问题has_many :through
和多态关联,如图所示这里 。 Though, in Rails 4, this is solved. 虽然,在Rails 4中,这已经解决了。
PS: Since I did some research on this question, I might as well post the answer in case someone can benefit from it... :) PS:由于我对这个问题进行了一些研究,我不妨发布答案,万一有人可以从中受益...... :)
Figured this one out myself, just answering because I'm sure I'm not the first or last person to make this stupid mistake (in fact I may have done it before). 我自己想出这个,只是回答,因为我确定我不是第一个或最后一个犯这个愚蠢错误的人(事实上我之前可能已经做过)。 I put the column type on the taggable_type field as an integer instead of a string. 我将taggable_type字段的列类型作为整数而不是字符串。
You'd think this might cause an error, but it doesn't. 您认为这可能会导致错误,但事实并非如此。 It just doesn't work. 它只是不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.