1

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?

zoum26
  • 124
  • 1
  • 2
  • 12
  • 1
    `Inscription.includes(item1: { item2: :banner })` The names `item1`, `item2` and `banner` need to match the names given to each relation (ex: if you set `Inscription has_many: :items2` then you should use `.includes(:items2)`). (You can use `.joins` with the same arguments if you want) – MrYoshiji Jun 17 '14 at 14:19
  • Thanks! @MrYoshiji If I want to add a where(name == "") how should I do it? – zoum26 Jun 17 '14 at 14:31
  • 1
    Just add `.where(name: "")` to the end, after all the `joins`/`includes`. – Joe Kennedy Jun 17 '14 at 14:34
  • It's not working :/ it says Unknown column 'inscriptions.name' in 'where clause' I wrote: Inscription.joins(firm_office_1: { firm: :banner }).where(name: "") – zoum26 Jun 17 '14 at 15:40

1 Answers1

2

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:

Community
  • 1
  • 1
MrYoshiji
  • 54,334
  • 13
  • 124
  • 117