如何includes
在保持即时加载的同时向活动记录中生成的 ON 子句添加条件?
假设我有这些课程:
class Car
has_many :inspections
end
class Inspection
belongs_to :car
end
现在我可以这样做:
Car.includes(:inspections)
Select * from cars LEFT OUTER JOIN inspections ON cars.id = inspections.car_id
但我想生成这个sql:
Select * from cars LEFT OUTER JOIN inspections ON cars.id = inspections.car_id
AND inspections.month = '2013-04-01'
(这不起作用):
Car.includes(:inspections).where("inspections.month = 2013-04-01")
Select * from cars LEFT OUTER JOIN inspections ON cars.id = inspections.car_id
WHERE inspections.month = '2013-04-01'