Join multiple tables with active records
Solution 1:
You can do the following:
Inscription.includes(item1: { item2: :banner })
The relations names item1
, item2
and banner
need to match the names given to each relation.
If you want to set a where
statement on this query, you can do:
scope = Inscription.includes(item1: { item2: :banner })
scope = scope.where(banner: { name: "MOTD: Hello World!" })
scope = scope.where(item2: { is_favorite: true })
Similar questions:
- Rails 4 scope to find parents with no children
- How to query a model based on attribute of another model which belongs to the first model?
- Rails active record querying association with 'exists'
- Rails 3, has_one / has_many with lambda condition