0

I use single table inheritance like this:

class Reservation < ApplicationRecord
end

class Stay < Reservation
end

Now I've got another class called room and I want it to be in many-to-many relation with the stay because reservation is more general. Moreover I don't need the association model so I want to use has_many_and_belongs to. So I created stay_rooms table:

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

and prepared models:

class Stay < Reservation
  has_and_belongs_to_many :rooms,
    join_table: :stay_rooms
end

class Room < ApplicationRecord
  has_and_belongs_to_many :stays, 
    class_name: 'Stay', 
    join_table: :stay_rooms
end

But it doesn't work and I keep getting "ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: stay_rooms.stay_id" when I try simple query: Stay.first.rooms. So I checked the docs and added foreign_key and association_key like this:

class Stay < Reservation
  has_and_belongs_to_many :rooms,
    foreign_key: 'room_id',  
    association_foreign_key: 'room_id',
    join_table: :stay_rooms
end

class Room < ApplicationRecord
  has_and_belongs_to_many :stays, 
    class_name: 'Stay', 
    foreign_key: 'reservation_id', 
    association_foreign_key: 'reservation_id',
    join_table: :stay_rooms
end

But it still didn't make it work and I don't know how to solve it.

UPDATE

I had a typo I mispelled foreign key and after correcting it it works but only one way. I can get rooms associated with stay but when I do it another way around it gives me an empty collection despite the fact I've got a room associated with a stay. (I did Stay.first.rooms << Room.first)

Gregg
  • 125
  • 1
  • 2
  • 11
  • `SQLite3::SQLException: no such column: stay_rooms.stay_id"` stay_rooms table doesn't has stay_Id column instead of that has reservation_id. Idk what are you trying to achieve, you should be more specific – Oscar Luza Sep 16 '17 at 22:36
  • I get this error when I try simple query like: Stay.first.rooms. I want the way to tell activerecord that the name of column in stay rooms is in fact reservation_id – Gregg Sep 17 '17 at 05:53
  • but your reference at stay_rooms table is `t.references :reservation, foreign_key: true, index: true`. Your table is called stay_rooms and you store reservation_id and room_id. You don't have that relationship, and since Stay < Reservation you can't handle the privilegios of inheritance. – Oscar Luza Sep 17 '17 at 06:00
  • I know but I use single table inheritance so every Stay is actually a Reservation with type Stay and every Stay has reservation_id. – Gregg Sep 17 '17 at 06:03
  • Every Stay is a Reservation but not every Reservation is a Stay. Do you know the principles of inheritance? – Oscar Luza Sep 17 '17 at 06:06
  • I sure do that's why I want a Stay to be associated with rooms and I want to achieve it via Stay class not Reservation. It's just I don't understand why rails uses stay_id which doesn't even exist I want a way to specifically tell it about reservation_id. As for inheritance I understand the concept and I know that you cannot treat child class as a parent class cause not every instance of it is the same and it may have different fields. In my case parent and child differ only on one field - type. – Gregg Sep 17 '17 at 06:12
  • @OscarLuza Okay I've figured out what was wrong I had a typo I mispelled `foreign_key`. And after I've corrected it the association works but unfortunately only one way. So when I do `Stay.first.rooms` I get the proper collection of rooms associated with a Stay but when I do `Room.first.stays` I get an empty collection which is not true because I've got one Room associated with one Stay. Any ideas how I could solve this? – Gregg Sep 17 '17 at 21:37

0 Answers0