0
我有一个模型AvailableSlot属性为“max_attendees”。 AvailableSlot has_many BookedSlots。如何根据子元素的条件检索记录?
我要寻找那些仍然可用的所有AvailableSlots(其中booked_slots小于max_attendees“)
我试图
scope :with_capacity, -> { joins('LEFT OUTER JOIN booked_slots on booked_slots.available_slot_id = available_slots.id')
.group('available_slots.id').having("booked_slots.count < available_slots.max_attendees") }
和
#AvailableSlot.rb
def self.with_capacity
self.select{ |s| s.booked_slots.count < s.max_attendees }
end
但这些返回数组,并且第一个不能执行“.count”,而第二个解决方案不能执行“.limit(3)”,因为返回的数据不是活动记录。
最后一个问题:对于做这样的事情,
AvailableSlot.joins(:booked_slots).where("booked_slots.length < ?, max_attendees)
我不能使用。长度,因为这不是在booked_slots列。在使用像.count,.length等activerecord方法时,使用这种.joins.where格式的方法是什么?