【发布时间】:2014-03-04 02:15:58
【问题描述】:
我有国家、城市、商店模型
class Country < ActiveRecord::Base
has_many :cities
end
class City < ActiveRecord::Base
belongs_to :country
has_many :shops
end
class Shop < ActiveRecord::Base
belongs_to :city
end
如何在 activerecord 中获取 country.shops? (获取国家/地区的所有商店)
我通常使用 Country.cities.collect { |c| c.商店} 但这不是 activerecord 对象。
我考虑过在 shop 模型上添加 country_id 并设置 has_many 关系,但我认为这不是最好的方法。
【问题讨论】:
标签: ruby-on-rails activerecord