1
我有這3款具有的has_many通過關係:Rails - 爲什麼在這種情況下Eager加載不起作用?
class Wine < ActiveRecord::Base
has_many :varietals, :conditions => "varietals.status = 1"
has_many :grapes, :through => :varietals
end
class Grape < ActiveRecord::Base
has_many :varietals
has_many :wines, :through => :varietals
end
class Varietal < ActiveRecord::Base
belongs_to :wine
belongs_to :grape
end
@records = Wine.where(conditions).includes({:varietals => :grape})
但是,如果我有10種葡萄酒,我會看到我的日誌10個請求像這些的主要葡萄酒請求後:
Grape Load (0.6ms) SELECT `grapes`.* FROM `grapes` INNER JOIN `varietals` ON `grapes`.id = `varietals`.grape_id WHERE ((`varietals`.wine_id = '98DF2CEC-61CC-4B22-B40D-620AADF650D2') AND ((varietals.status = 1)))
如何在主要請求中加載葡萄,以避免每個葡萄的請求?
這不起作用。 – 2014-05-15 17:47:43