Rails多态关联是插入一个额外的字段

时间:2013-09-09 23:13:34

标签: ruby-on-rails polymorphic-associations

我创建了一个迁移来更改我的数据模型,以使我的Conversation模型具有多态性。

以下是我的文件,缩小了:

# deal.rb
class Deal < ActiveRecord::Base
  has_many :conversations, as: :conversationable
end

# conversation.rb
class Conversation < ActiveRecord::Base
    belongs_to :conversationable, polymorphic: true
end

在我的seeds.rb中,我在说这句话:

conversation = deal.conversations.create!(:seller_id => deal.user.id, :buyer_id => user.id)

哪会产生错误:

SQLite3::SQLException: table conversations has no column named deal_id: INSERT INTO "conversations" ("buyer_id", "conversationable_id", "conversationable_type", "created_at", "deal_id", "seller_id", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?)

为什么rails仍在插入deal_id?我必须删除其他地方的协会遗留物吗?

在rails控制台中运行相同的代码时,将使用正确的关联创建模型。

谢谢。

1 个答案:

答案 0 :(得分:0)

想出来(感谢Ryan Biggs)。

问题是要重置我的应用程序,我正在调用

rake db:drop && rake db:migrate && rake db:seed

没有足够的时间在迁移和播种之间重新加载列信息。

相关问题