2012-03-29 55 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)")