Pluck associated model's attribute in Rails query

后端 未结 2 1949
忘了有多久
忘了有多久 2021-02-19 03:13

In my rails app, collections have many projects, and projects have many steps.

I\'d like to grab all the ids of steps

2条回答
  •  猫巷女王i
    2021-02-19 03:19

    Unfortunately, I don't think that we could do it through AR in a single query. You could do a nested query below to retrieve it in two queries to the database:

    Step.includes(:projects)
        .where(projects: { id: Projects.includes(:collections)
        .where(collections: { id: @collections.id }).pluck(:id) } )
        .pluck(:id)
    

提交回复
热议问题