ActiveRecord Join Query and select in Rails

前端 未结 5 1771
甜味超标
甜味超标 2021-01-30 10:34

In my rails 4 application, a client (clients table) can have many projects (projects table). I have a column called name in each table. I am trying to write a

5条回答
  •  说谎
    说谎 (楼主)
    2021-01-30 11:03

    If the column in select is not one of the attributes of the model on which the select is called on then those columns are not displayed. All of these attributes are still contained in the objects within AR::Relation and are accessible as any other public instance attributes.

    You could verify this by calling first.client_name:

    Project.joins(:client)
           .select('projects.id,projects.name,clients.name as client_name')
           .first.client_name
    

提交回复
热议问题