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

时间:2015-11-10 20:27:29

标签: ruby-on-rails activerecord polymorphic-associations

图书模型是

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

Taggings Model是

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" = ?

1 个答案:

答案 0 :(得分:0)

您是否为协会创建了所有必要的列? 您的标记表的架构应该与此

类似
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列或忘记执行迁移。