我想要做的是在between..Here的抽象使用两个中介模型加入一个模型到另一个:如何在两个模型之间创建has_many关系,其中有几个模型? (Ruby on Rails的ActiveRecord的)
Country has_many Companies Company has_many Buildings, Company belongs_to Country Building has_many Rooms, Building belongs_to Company Room belongs_to Building
我希望能够做Country.first .rooms,所以我想作为全国模型应该是简单的:
class Country - ActiveRecord::Base has_many :companies has_many :buildings, :through=>:companies has_many :rooms, :through=>:buildings end
然而,这种尝试生成SQL这样的: SELECT * FROM rooms
INNER JOIN buildings
ON rooms
.building_id = building
.ID WHERE(( building
.country_id = 1))
显然,building.country_id不存在......我如何解决这个问题?
什么是您的Rails(特别是ActiveRecord)版本? – steel 2016-04-24 02:04:40