zoum26
zoum26

Reputation: 124

Join multiple tables with active records

I want to know the banner name situated in a great grandchildren tab. Here's how my data base is:

Inscription (where I need to start from): Item1_id
Item1: Item2_id
Item2: Banner_id
Banners: name

How do i get all the banners name of my inscription table with active records?

Upvotes: 1

Views: 1457

Answers (1)

MrYoshiji
MrYoshiji

Reputation: 54882

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:

Upvotes: 2

Related Questions