Querying has_many Associations

This is a companion discussion topic for the original entry at https://thoughtbot.com/upcase/videos/advanced-querying-has-many.

This is great stuff! So, you mean to tell me ActiveRecord will only make 1 query for all this?!

I’ve literally implemented very similar queries using Enumerable… This is a total game changer!

Hi, thanks for this tutorial!

Just to let you know that the tables names in the examples under the ‘Gluing tables together with the joins method’ section are mismatched; for instance the locations table is labelled ‘people’

Thanks

Andrea

Is there any way of merging more than one queries without being longwinded?

People.joins(location: :region).merge(Region.order(:name)).merge(Location.order(:name)).order(:name)

Does this work?

People.joins(location: :region).merge(Region.order(:name), Location.order(:name)).order(:name)

@JalenOng

it’s possible to rewrite the query using this format, it’s based on @christoomey solution https://exercises.upcase.com/exercises/advanced-activerecord-querying-belongs_to-associations/solutions/christoomey :
People.joins(location: :region).order("regions.name, locations.name, name")