我想知道我们是否可以在模型级别急切加载:
# country.rb
class Country < ActiveRecord::Base
has_many :country_days
def country_highlights
country_days.map { |country_day| country_day.shops }.flatten.uniq.map { |shop| shop.name }.join(", ")
end
end
# country_day.rb
class CountryDay < ActiveRecord::Base
belongs_to :country
has_many :country_day_shops
has_many :shops, :through => :country_day_shops
end
# shop.rb
class Shop < ActiveRecord::Base
end
大多数时候,由于某些多态关联,很难.includes
在控制器中使用。无论如何,我是否可以country_highlights
在模型级别急切加载该方法,这样我就不必.includes
在控制器中添加?