I have a car model. (In my database, the cars table has a column named “customer_id“.)
class Car < ActiveRecord::Base
belongs_to :customer
end
A customer model: (The customer model has a attribute named “customer_name“.)
class Customer < ActiveRecord::Base
has_many :cars
end
My question is, if I queried the cars table with:
where("id >= ? AND id <= ?", 3, 15) #get some cars
Then I would like to order this result by customer_name ascending order, what’s the ordering code??
I tried both
where("id >= ? AND id <= ?", 3, 15).order("customer_id.customer_name ASC")
and
where("id >= ? AND id <= ?", 3, 15).order("customer.customer_name ASC")
But seems neither of them is working. Any body can help? (I am using Rails 3)
You need to
jointhe custom table for this query to work.Try