I apologise for the contrived example
A person
has ONE journey
but is not directly connected to that journey
, instead, they are connected via their house
and office
.
A person belongs_to a house
, a house has_many people
A person belongs_to a office
, a office has_many people
A journey belongs_to a house
, a house has many journeys
A journey belongs_to a office
, a office has many journeys
+---------+
+---Belongs-To----> <----Belongs-To--+
| | House | |
| +-Has-Many-----+ +-----Has-Many-+ |
| | +---------+ | |
| | | |
+------+--v+ +v-+--------+
| | | |
| Person | | Journey |
| | | |
+------+--^+ +^-+--------+
| | | |
| | +----------+ | |
| +-Has-Many-----+ +----Has-Many-+ |
| | Office | |
+----Belongs-To---> <---Belongs-To--+
+----------+
Following that contrived example, what's the best way to allow the following:
person.journey
OR baring that a Join query with multiple tables (using ruby hashes) or using a has_one_through
with extra table constraints.
We do have a sql query but we'd rather avoid using raw sql if we can, but it looks like this:
Person
.joins('INNER JOIN journeys
ON journeys.office_id = person.office_id
AND journeys.house_id = person.house_id')