0

我的模型设置如下:

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

这就是我渴望为一个国家/地区加载manufacturerscars包括汽车的 attr 访问器)的方式:

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

在上述调用中也急切加载comfort levelsfor的语法是什么?cars

我尝试了各种事情,但到目前为止还没有运气。

非常感谢这方面的任何帮助。谢谢!

4

2 回答 2

0

你试过这个吗?

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

于 2013-08-08T21:04:31.207 回答
0

我终于能够急切地加载所有内容,使用:

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

上面的调用没有在记录中嵌套comfort level记录car,但是,以下调用:

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

这个链接帮助我清除了急切加载语法的一些概念。

于 2013-08-10T02:09:09.053 回答