Cześć, korzystam z STI w taki sposób:
class Reservation < ApplicationRecord
end
class Reservation::Stay < Reservation
end
i mam do tego taką migrację:
class CreateReservations < ActiveRecord::Migration[5.1]
  def change
    create_table :reservations do |t|
      t.datetime :reservation_date
      t.integer :status, default: 0
      t.float :additional_costs
      t.string :type
      t.references :user, foreign_key: true
      t.timestamps
    end
  end
end
Chciałbym, żeby tylko Reservation::Stay czyli dziecko Reservation był w rejacji many-to-many z kolejnym modelem - Room. Nie potrzebuję modelu pośredniego, więc chcę zastosować has_many_and_belongs_to. Mam do tego taką migrację:
class CreateStayRooms < ActiveRecord::Migration[5.1]
  def change
    create_table :stay_rooms, id: false do |t|
      t.references :reservation, foreign_key: true, index: true
      t.references :room, foreign_key: true, index: true
    end
  end
end
zgodnie z dokumentacją wprowadziłem foregin_key and association_foregin_key w obu modelach:
class Reservation::Stay < Reservation
  has_and_belongs_to_many :rooms,
    foregin_key: 'room_id',  
    association_foregin_key: 'room_id',
    join_table: :stay_rooms
end
class Room < ApplicationRecord
  has_and_belongs_to_many :stays, 
    class_name: 'Reservation::Stay', 
    foregin_key: 'reservation_id', 
    association_foregin_key: 'reservation_id',
    join_table: :stay_rooms
  has_many :room_options
  has_many :options, through: :room_options
end
Niestety po utworzeniu Reservation::Stay i próbie wykonania Reservation::Stay.first.rooms dostaję taki błąd:
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: stay_rooms.stay_id
Nie wiem w jaki sposób powiedzieć ActiveRecord, żeby używał reservation_id. Czy ktoś wie jak rozwiązać ten problem?
, teraz to wygląda tak: