rails select and include

前端 未结 5 1342
清歌不尽
清歌不尽 2021-02-19 02:02

Can anyone explain this?

Project.includes([:user, :company])

This executes 3 queries, one to fetch projects, one to fetch users for those proje

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-19 02:12

    Rails has always ignored the select argument(s) when using include or includes. If you want to use your select argument then use joins instead.

    You might be having a problem with the query gem you're talking about but you can also include sql fragments using the joins method.

    Project.select("name").joins(['some sql fragement for users', 'left join companies c on c.id = projects.company_id'])
    

    I don't know your schema so i'd have to guess at the exact relationships but this should get you started.

提交回复
热议问题