2012-03-29 54 views

回答

3

我会做按照下面的模型....

@cars_without_owners = Car.where("owner_id = ?", nil) 

或者是安全....

@cars_without_owners = Car.where("owner_id = ? OR owner_id = ?", nil, "") 
1

你可以利用这一点,虽然这将是非常如果您的表格有很多记录,则会变慢:

Car.where("not exists (select o.id from owners as o where o.car_id = cars.id)")