rails select and include

前端 未结 5 1313
清歌不尽
清歌不尽 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条回答
  •  误落风尘
    2021-02-19 02:20

    Allright so here's what I came up with...

    .joins("LEFT JOIN companies companies2 ON companies2.id = projects.company_id LEFT JOIN project_types project_types2 ON project_types2.id = projects.project_type_id LEFT JOIN users users2 ON users2.id = projects.user_id") \
    .select("six, fields, I, want")
    

    Works, pain in the butt but it gets me just the data I need in one query. The only lousy part is I have to give everything a model2 alias since we're using meta_search, which seems to not be able to figure out that a table is already joined when you specify your own join conditions.

提交回复
热议问题