What I'd like to do is join one model to another using two intermediary models in between..Here's the abstraction:
Country has_many Companies Company has_many Buildings, Company belongs_to Country Building has_many Rooms, Building belongs_to Company Room belongs_to Building
I want to be able to do Country.first.rooms, so I thought the Country model would be as simple as:
class Country < ActiveRecord::Base
has_many :companies
has_many :buildings, :through=>:companies
has_many :rooms, :through=>:buildings
end
However, this tries to generate SQL like:
SELECT * FROM rooms
INNER JOIN buildings
ON rooms
.building_id = building
.id WHERE ((building
.country_id = 1))
Obviously, building.country_id does not exist. How do I get around this?