[英]No column error in Many to many polymorphic association in rails
Book Model is 图书模型为
class Book < ActiveRecord::Base
has_many :tokens, :through => :taggings
has_many :taggings, :as => :taggable
end
Token Model is 令牌模型现为
class Token < ActiveRecord::Base
has_many :books, :through => :taggings, :source => :taggable, :source_type => "Book"
has_many :taggings
end
Taggings Model is 标记模型现为
class Tagging < ActiveRecord::Base
belongs_to :token
belongs_to :taggable, :polymorphic => true
end
When I get Book.first.tokens OR Token.first.books It gives error 当我得到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" = ?
Have you created all the necessary columns for your associations? 您是否为关联创建了所有必要的列? Your schema for your taggings table should look similar to this 您的标签表架构应与此类似
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
I think you are missing the token_id
column or forgot to perform a migration. 我认为您缺少token_id
列或忘记了执行迁移。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.