ErwinM
ErwinM

Reputation: 5131

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?

Upvotes: 13

Views: 4806

Answers (1)

valodzka
valodzka

Reputation: 5805

Project.joins(:customer).order('customers.name')

Upvotes: 30

Related Questions