你能不能使用ActiveRecord添加连接条件?
例如,我有一个非常复杂的查询使用几个依赖记录,它通过组合条件和包含指令工作正常
Contractors.find( :all, :include => {:council_areas => :suburbs}, :conditions => ["suburbs.postcode = ?", customer.postcode] )
假如说:
承包商有很多议员
CouncilAreas拥有很多郊区
此连接返回customer.postcode标识的郊区的承包商.
生成的查询如下所示:
SELECT contractors.*, council_areas.*, suburbs.* FROM `contractors` LEFT OUTER JOIN `contractors_council_areas` ON `contractors_council_areas`.contractor_id = `contractors`.id LEFT OUTER JOIN `council_areas` ON `council_areas`.id = `contractors_council_areas`.council_area_id LEFT OUTER JOIN `council_areas_suburbs` ON `council_areas_suburbs`.council_area_id = `council_areas`.id LEFT OUTER JOIN `suburbs` ON `suburbs`.id = `council_areas_suburbs`.suburb_id WHERE (suburbs.postcode = '5000')
(注意:为简洁起见,我编辑了列列表).
你能不能使用ActiveRecord添加连接条件?
例如,我有一个非常复杂的查询使用几个依赖记录,它通过组合条件和包含指令工作正常
Contractors.find( :all, :include => {:council_areas => :suburbs}, :conditions => ["suburbs.postcode = ?", customer.postcode] )
假如说:
承包商有很多议员
CouncilAreas拥有很多郊区
此连接返回customer.postcode标识的郊区的承包商.
生成的查询如下所示:
SELECT contractors.*, council_areas.*, suburbs.* FROM `contractors` LEFT OUTER JOIN `contractors_council_areas` ON `contractors_council_areas`.contractor_id = `contractors`.id LEFT OUTER JOIN `council_areas` ON `council_areas`.id = `contractors_council_areas`.council_area_id LEFT OUTER JOIN `council_areas_suburbs` ON `council_areas_suburbs`.council_area_id = `council_areas`.id LEFT OUTER JOIN `suburbs` ON `suburbs`.id = `council_areas_suburbs`.suburb_id WHERE (suburbs.postcode = '5000')
(注意:为简洁起见,我编辑了列列表).