Answer the question
In order to leave comments, you need to log in
How to sort related tables correctly?
If I want to sort all contracts by department name, am I doing the right thing?
@contracts = Contract.includes(:department).order('departments.name asc')
class Department < ActiveRecord::Base
has_many :contracts
end
class Contract < ActiveRecord::Base
belongs_to :department
end
Answer the question
In order to leave comments, you need to log in
Contract.joins(:department).order('departments.name asc')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question