Ruby on rails Rails—为什么在这种情况下不使用即时加载?
我有这3种型号,它们之间有着千丝万缕的联系:Ruby on rails Rails—为什么在这种情况下不使用即时加载?,ruby-on-rails,eager,Ruby On Rails,Eager,我有这3种型号,它们之间有着千丝万缕的联系: 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,
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})
如何在主请求中加载grapes以使每个grapes都没有请求?尝试以下方法:
Wine.includes(:varietals).includes(:grapes).where(conditions)
如果有效,不要问我为什么;)
Wine.includes(:varietals).includes(:grapes).where(conditions)