我有这些模型
class Car < ApplicationRecord
has_many :car_locations ,:dependent=>:destroy
has_many :rides,:dependent=>:destroy
end
class CarLocation < ApplicationRecord
belongs_to :car
end
class Ride < ApplicationRecord
belongs_to :car
end
status
Ride
类型的模型中有 boolean
属性。
现在我希望那些CarLocation
的车辆乘坐状态为false
,或者没有最低限度的车辆乘坐。
答案 0 :(得分:2)
我希望那些乘坐
status
的车位是false
CarLocation.joins(car: :rides).where(rides: { status: false })
或者没有任何车程
CarLocation.includes(car: :rides)
.where(rides: { id: nil })
.references(:rides)
将两者结合在一起:
CarLocation.includes(car: :rides)
.where('rides.status = false OR rides.id IS NULL')
.references(:rides)