Wednesday, 14 August 2013

Rails active record querying association with 'exists'

Rails active record querying association with 'exists'

I am working on an app that allows Members to take a survey (Member has a
one to many relationship with Response). Response holds the member_id,
question_id, and their answer.
The survey is submitted all or nothing, so if there are any records in the
Response table for that Member they have completed the survey.
My question is, how do I write the re-write the query below so that it
actually works? In SQL this would be a prime candidate for the EXISTS
keyword
def surveys_completed
members.where(responses: !nil ).count
end

No comments:

Post a Comment