I've been out of the rails dev world for a little while and am a bit rusty when it comes to setting up relationships and joins between tables, so apologies if this is a very simple question.
- I have a
Team
model which has anid
column. - I have a
Match
model which hashome_team_id
andaway_team_id
columns. - I'd like to be able to call
@team.matches
and have anyMatch
where either thehome_team_id
oraway_team_id
equal the@team.id
show up.
I can achieve it with the following query, but it's far from ideal.
@matches = Match.where(home_team_id: @team.id) || Match.where(away_team_id: @team.id))
So what's the recommended way to set up my models and relationships to achieve the above behaviour?
Thanks in advance!