rails 中的多对多多态关联中没有列错误

No column error in Many to many polymorphic association in rails

图书模型是

class Book < ActiveRecord::Base
has_many :tokens, :through => :taggings
has_many :taggings, :as => :taggable
end

代币模型是

class Token < ActiveRecord::Base
has_many :books, :through => :taggings, :source => :taggable, :source_type => "Book"
has_many :taggings
end

标签模型是

class Tagging < ActiveRecord::Base
belongs_to :token
belongs_to :taggable, :polymorphic => true
end

当我得到 Book.first.tokens 或 Token.first.books 时出现错误

ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: taggings.token_id: SELECT "tokens".* FROM "tokens" INNER JOIN "taggings" ON "tokens"."id" = "taggings"."token_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ?

您是否为您的协会创建了所有必要的栏目? 您的标签架构 table 应该类似于此

create_table "taggings", force: :cascade do |t|
    t.integer  "token_id",                  limit: 4
    t.integer  "taggable_id",               limit: 4
    t.string   "taggable_type",             limit: 255
    t.datetime "created_at",                null: false
    t.datetime "updated_at",                null: false
  end

我认为您缺少 token_id 列或忘记执行迁移。