Ruby on rails 多元关联,并且有很多:通过在试图获取关联记录时生成错误的sql,rails?

Ruby on rails 多元关联,并且有很多:通过在试图获取关联记录时生成错误的sql,rails?,ruby-on-rails,polymorphism,Ruby On Rails,Polymorphism,为了说明这一点,我创建了一个多态的标记关联,它属于microspost和标记 编辑的标记模型错误 class Tagging < ApplicationRecord belongs_to :tag, inverse_of: :taggings belongs_to :taggable, polymorphic: true ... 它将tag_id放入taggable_id中,该id实际上应该是micropost id,因此给出空数组 我的怀疑是,正如您在生成的sql中所看到的

为了说明这一点,我创建了一个多态的
标记
关联,它属于
microspost
标记

编辑的标记模型错误

class Tagging < ApplicationRecord

  belongs_to :tag, inverse_of: :taggings
  belongs_to :taggable, polymorphic: true
...

它将
tag_id
放入
taggable_id
中,该id实际上应该是micropost id,因此给出空数组

我的怀疑是,正如您在生成的sql中所看到的,它显示了
[“taggable_类型”,“Tag”],[“taggable_类型”,“Micropost”]
其中
Tag
taggable_类型不应该存在,但我不知道如何摆脱它

为清晰起见,我添加了编辑模式

create_table "taggings", force: :cascade do |t|
  t.string "taggable_type"
  t.bigint "taggable_id"
  t.bigint "tag_id"
  t.string "topic"
  t.datetime "created_at", precision: 6, null: false
  t.datetime "updated_at", precision: 6, null: false
  t.index ["tag_id"], name: "index_taggings_on_tag_id"
  t.index ["taggable_type", "taggable_id"], name: "index_taggings_on_taggable_type_and_taggable_id"
  t.index ["topic"], name: "index_taggings_on_topic"
end

create_table "tags", force: :cascade do |t|
  t.string "name"
  t.datetime "created_at", precision: 6, null: false
  t.datetime "updated_at", precision: 6, null: false
  t.index ["name"], name: "index_tags_on_name"
end

所以丢失了。

更改标记中的关系

类标记
具有其他模型id列的模型属于该模型。
ER模型如下所示:

Micropost ---------> Tagging <------------ Tag
                   :taggable_type
                   :taggable_id
                   :tag_id

microspost-------->标记OK。我意识到我已经在
标签中定义了这个模型

has_many :taggings, as: :taggable, inverse_of: :taggable
应该是

has_many :taggings
因为我们需要tags.taggings而不是tag.taggings作为taggable,它现在将查询
tag\u id
,并为tag.microspots生成正确的sql


Thnx

等等,我把代码搞糟了,我重新编辑了
标签
模型
<Tag id: 10, name: "hello", created_at: "2020-04-01 08:39:36", updated_at: "2020-04-01 08:39:36">
tag.microposts
Micropost Load (0.4ms)  SELECT "microposts".* FROM "microposts" INNER JOIN "taggings" ON "microposts"."id" = "taggings"."taggable_id" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 AND "taggings"."taggable_type" = $3 LIMIT $4  [["taggable_id", 10], ["taggable_type", "Tag"], ["taggable_type", "Micropost"], ["LIMIT", 11]]
=> <ActiveRecord::Associations::CollectionProxy []>
create_table "taggings", force: :cascade do |t|
  t.string "taggable_type"
  t.bigint "taggable_id"
  t.bigint "tag_id"
  t.string "topic"
  t.datetime "created_at", precision: 6, null: false
  t.datetime "updated_at", precision: 6, null: false
  t.index ["tag_id"], name: "index_taggings_on_tag_id"
  t.index ["taggable_type", "taggable_id"], name: "index_taggings_on_taggable_type_and_taggable_id"
  t.index ["topic"], name: "index_taggings_on_topic"
end

create_table "tags", force: :cascade do |t|
  t.string "name"
  t.datetime "created_at", precision: 6, null: false
  t.datetime "updated_at", precision: 6, null: false
  t.index ["name"], name: "index_tags_on_name"
end
Micropost ---------> Tagging <------------ Tag
                   :taggable_type
                   :taggable_id
                   :tag_id
has_many :taggings, as: :taggable, inverse_of: :taggable
has_many :taggings