An event has many participants. A participant has a field of "status".
class Event < ActiveRecord::Base
has_many :participants
end
class Participant < ActiveRecord::Base
belongs_to :event
end
I need to find all events except the following ones: events where every one of its participants has a status of 'present'.
I can find all events where some of its participants have a status of 'present' with the following AR code:
Event.joins(:participants).where
.not(participants: {status: 'present'})
.select("events.id, count(*)")
.group("participants.event_id")
.having("count(*) > 0")
That creates SQL like:
SELECT events.id, participants.status as status, count(*)
FROM `events` INNER JOIN `participants`
ON `participants`.`event_id` = `events`.`id`
WHERE (`participants`.`status` != 'present')
GROUP BY participants.event_id HAVING count(*) > 0
This almost works. The problem is that if one of the participant's rows (within the scope of @participant.event_id
) has a status of something other like "away", the event will still get fetched, because at least some of the sibling records are of a status equal to something other than "present".
I need to ensure that I am filtering out every event record with all participants of a status of "present".
I am open to ActiveRecord or SQL solutions.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…