ActiveRecord Join Query and select in Rails

前端 未结 5 1778
甜味超标
甜味超标 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:04

    your query don't looses any thing. Actually you have applied join on models and you have written Project.joins(:client) that why it is looking like. means It will hold Project related data as it is and associated data hold with alias name that you have given 'client_name' in your query.

    if you use

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

    then it look like [#, #]

    but it hold all the attribute that you selected.

提交回复
热议问题