【问题标题】:No column error in Many to many polymorphic association in railsrails中的多对多多态关联中没有列错误
【发布时间】:2015-11-10 20:27:29
【问题描述】:

图书模型是

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

【问题讨论】:

  • 您的模型是否包含token_id 列?

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


【解决方案1】:

您是否为您的关联创建了所有必要的列? 您的 taggings 表的架构应该类似于此

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多