Join multiple tables with active records

前端 未结 1 582
天命终不由人
天命终不由人 2020-11-28 16:26

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:          


        
相关标签:
1条回答
  • 2020-11-28 16:44

    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
    0 讨论(0)
提交回复
热议问题