Ruby on rails ActiveRecord::语句无效:PG::UndefinedTable:错误:与HABTM的关系与rails的关联

Ruby on rails ActiveRecord::语句无效:PG::UndefinedTable:错误:与HABTM的关系与rails的关联,ruby-on-rails,activerecord,migration,ruby-on-rails-5,has-and-belongs-to-many,Ruby On Rails,Activerecord,Migration,Ruby On Rails 5,Has And Belongs To Many,我有两个模型企业和处理与habtm协会的活动。这种关系与create和when-I起作用 Enterprise.last.deal_events 但会抛出错误 ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "deal_events_enterprises" does not exist LINE 1: ...CT "enterprises".* FROM "enterprises" INNER JOI

我有两个模型企业和处理与habtm协会的活动。这种关系与create和when-I起作用

Enterprise.last.deal_events
但会抛出错误

ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "deal_events_enterprises" does not exist LINE 1: ...CT  "enterprises".* FROM "enterprises" INNER JOIN "deal_even...
当我这样做的时候

DealEvent.last.enterprises
我的模型

enterprise.rb

has_and_belongs_to_many :deal_events
deal_event.rb

has_and_belongs_to_many :enterprises
迁移

class CreateDealEventsEnterprisesJoinTable < ActiveRecord::Migration[5.1]
  def change
    create_join_table :deal_events, :enterprises do |t|
      t.index :deal_event_id
      t.index :enterprise_id
    end
  end
end
class CreateDealEventsEnterprisesJoinTable
您应该将联接表命名为
deal\u event\u enterprises
(注意不要使用复数deal\u event)。您的迁移看起来像这样

class CreateDealEventEnterprises < ActiveRecord::Migration[5.1]
  def change
    create_table :deal_event_enterprises do |t|
      t.integer :deal_event_id
      t.integer :enterprise_id
    end
  end
end
class DealEventEnterprise < ApplicationRecord
  belongs_to :deal_event
  belongs_to :enterprise
end

class Enterprise < ApplicationRecord
  has_many :deal_event_enterprises
  has_many :deal_events, through: :deal_event_enterprises
end

class DealEvent < ApplicationRecord
  has_many :deal_event_enterprises
  has_many :enterprises, through: :deal_event_enterprises
end