简体   繁体   English

为什么此多态关联的source_type始终为0?

[英]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.

相关问题 为什么 Rails 中设置了 source_type 的多态关联会导致错误的 SQL 语句? - Why does a polymorphic association in Rails with source_type set result in the wrong SQL statement? Rails has_many通过多态source_type scope_chain错误 - Rails has_many through polymorphic source_type scope_chain bug Rails中多态关联中的更改类型 - change type in polymorphic association in rails 如果多态关联的类型列不指向 STI 的基本模型,为什么多态关联对 STI 不起作用? - Why polymorphic association doesn't work for STI if type column of the polymorphic association doesn't point to the base model of STI? Rails has_many通过使用source和source_type别名来处理多种类型 - Rails has_many through aliasing with source and source_type for multiple types 按视图类型过滤多态关联 - Filtering polymorphic association by type for a view 获取特定类型的多态关联 - Get specific type of polymorphic association 在多态关联中更改类型标识符 - Change type identifier in polymorphic association 为什么我的多态关联为零,而_id和_type字段却为零? - Why is my polymorphic association nil while the _id and _type fields are not? has_many:through,:source,:source_type返回空数组 - has_many :through, :source, :source_type returning empty array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM