Let’s say I have three models, Company, User and Entry (and a join model EntryParticipants).
Company has many Users, a User has many Entries (Entries created by a User).
Also, there is another many-to-many relationship between User and Entry (a User can participate in many entries and an Entry can have many participants).
So, now I want to fetch all Entries that a User has created OR is participating in. I’m a bit stuck though…
user.entries => All entries created by this user
user.participated_entries => All participated entries
But now I want to get them all in one query. The code below is working, but is there a more efficient way to do it?
scope.includes(:entry_participants).where("(entry_participants.participated_entry_id = entries.id) OR entries.user_id = ?", user.id)