ActiveRecord includes. Specify included columns

后端 未结 6 458
野的像风
野的像风 2020-12-02 17:08

I have model Profile. Profile has_one User. User model has field email. When I call

Profile.some_scope.includes(:user)

it calls

<         


        
6条回答
  •  既然无缘
    2020-12-02 17:38

    If you want to select specific attributes, you should use joins rather than includes.

    From this asciicast:

    the include option doesn’t really work with the select option as we don’t have control over how the first part of the SELECT statement is generated. If you need control over the fields in the SELECT then you should use joins over include.

    Using joins:

    Profile.some_scope.joins(:users).select("users.email")
    

提交回复
热议问题