Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 sorting through parent association

I have a fairly straightforward question in Rails 3 to which I can't seem to find the answer:

Let's say I have 2 models: Customer, Project.

A Customer has_many projects
Project belongs_to customer

Now I want to sort a list of projects by "customer name" using Active Record (doing it with a Ruby array is easy enough, but I imagine this will get problematic once the number of project records grows).

How do I go about doing this with ActiveRecord?

like image 229
ErwinM Avatar asked Feb 03 '11 20:02

ErwinM


1 Answers

Project.joins(:customer).order('customers.name')
like image 166
valodzka Avatar answered Nov 06 '22 02:11

valodzka