I have a seat object that has a car object that has a owner that has a name. I want to display the car brand and the car‘s owner‘s name together. How do I do this in one query?
eg:
class Seat < ActiveRecord::Base
belongs_to :car
def description
"I am in a #{car.brand} belonging to #{car.owner.name}"
# --> how do I replace this with one query?
end
end
I’ll note that this is a highly contrived example to simplify my question. I’m doing this thousands of times in a row, hence the need for more efficiency.
Let us say you are trying to query the
Seatmodel, and you want to eager load the car and owner objects, you can use theincludesclause.