Activerecord, Join, Select *

After much searching I have been unable to find the correct information on selecting * between a table join.

Here is the query I am dealing with:

Assignment.joins(“LEFT JOIN courses ON courses.id = assignments.context_id LEFT JOIN enrollments ON enrollments.course_id = courses.id”).includes(:courses).where(:enrollments => {:user_id => ‘8’, :course_id => ‘1’, :workflow_state => ‘active’})

And I would like to select, assignments.* and courses., but joins only selects the table calling the joins, assignments.

Any help on this issue would be greatly appreciated. I feel like I am missing something simple

Can I ask if there is a good reason that you are doing it this way rather the Rails way using associations? If you do not know then tell us about the models and associations and what you are trying to achieve with the query.

Colin

You can use select method to define selectable fields like Assignment.select(‘assignments., courses.’) but you’ll get the array of Assignment objects with course columns.

More natural code is

Enrollment.where(user_id: 8, course_id: 1, workflow_state: ‘active’).preload(courses: [: assignments])

You’ll get array of Enrollment with loaded association objects (Assignment and Course). It takes 3 simple and fast sql queries.