代码之家  ›  专栏  ›  技术社区  ›  NolanDC Simon Lee

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

  •  0
  • NolanDC Simon Lee  · 技术社区  · 11 年前

    我创建了一个迁移来更改我的数据模型,以便使我的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 回复  |  直到 11 年前
        1
  •  0
  •   NolanDC Simon Lee    11 年前

    弄明白了(多亏了瑞安·比格斯)。

    问题是为了重置我的应用程序,我打电话

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

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