I have 3 models, “Coach”, “Team”, and “Event”
An event has attributes away_team and home_team, both of which belong to Team. A team belongs to a coach, and a coach can have many teams.
What I’d like to do is find all events where the home_team is not coached by a particular coach. So, something along the lines of:
Event.where("home_team.team.coach_id NOT ?", coach.id)
The problem is figuring out how to write up that syntax. I’m guessing I need to include the “team” model, but I’m not sure how to write an activerecord include that joins through a specific foreign key.
In short, any ideas on how to do this without getting SQL errors (which I’ve had countless of tonight) would be very appreciated.
Cheers.
If I understand you correctly you need this kind of sql:
so in rails it should looks something like that:
Of course I don’t know your exact table namings, so you should fit this solution to your problem.
If you have in Event model association: belogns_to: home_team, you can use only joins(:home_team).where(….)