سؤال

I have my models setup like this:

class Country < ActiveRecord::Base
   has_many :manufacturers
end

class Manufacturer < ActiveRecord::Base
   belongs_to :country
   has_many :cars
end

class Cars < ActiveRecord::Base
   belongs_to :manufactuer
   has_many :comfort_levels

   attr_accessor :attr_accessor_1, :attr_accessor_2
end

class ComfortLevel < ActiveRecord::Base
   belongs_to :car
end

This is how I am eager loading manufacturers with cars (including car's attr accessors) for a country:

data = current_country.manufacturers.to_json :include => {:cars => {:methods => [:attr_accessor_1, :attr_accessor_1]}}

What will be the syntax to also eager load the comfort levels for cars in the above call?

I have tried various things, but no luck so far.

Would greatly appreciate any help in this regard. Thanks!

هل كانت مفيدة؟

المحلول 2

I was finally able to eager load everything, using:

data = current_country.manufacturers.to_json :include => [{:cars => {:methods => [:attr_accessor_1, :attr_accessor_1]}}, :comfort_levels]

The above call did not nest comfort level records inside car records, however, the following did:

data = current_country.manufacturers.to_json :include => {:cars => {:methods => [:attr_accessor_1, :attr_accessor_1, :comfort_levels]}}

This link helped me clear some concepts around the syntax for eager loading.

نصائح أخرى

Have you tried this?

data = current_country.manufacturers.to_json :include => {:cars => {:methods => [:attr_accessor_1, :attr_accessor_1], :comfort_levels => {}}}

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top