rails select and include

前端 未结 5 1315
清歌不尽
清歌不尽 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:26

    I might be totally missing something here but select and include are not a part of ActiveRecord. The usual way to do what you're trying to do is like this:

    Project.find(:all, :select => "users.name", :include => [:user, :company], :joins => "LEFT JOIN users on projects.user_id = users.id")
    

    Take a look at the api documentation for more examples. Occasionally I've had to go manual and use find_by_sql:

    Project.find_by_sql("select users.name from projects left join users on projects.user_id = users.id")
    

    Hopefully this will point you in the right direction.

提交回复
热议问题