【问题标题】:Rails polymorphic association is inserting an extra fieldRails 多态关联正在插入一个额外的字段
【发布时间】:2013-09-09 23:13:34
【问题描述】:

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

这是我的文件,精简后:

# 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 控制台中运行相同的代码时,会创建具有正确关联的模型。

谢谢。

【问题讨论】:

  • 你确定它在你认为的地方失败了吗?听起来像是一个过时的夹具文件
  • 在 Deal 类中是否存在与没有 as 选项的对话的旧关联?
  • @firien:是的!我查看了日志并将其追踪到了那一行。
  • @tihorn:我也有这个想法 - 这是该课程中对话的唯一关联。
  • 您确定要运行迁移(在测试数据库中)吗?如果可以,你能展示一下吗?

标签: ruby-on-rails polymorphic-associations


【解决方案1】:

想通了(感谢 Ryan Biggs)。

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-23
    • 2014-10-12
    • 2011-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多