Rails find where ALL associated records meet condition

Viewed 2994

I'm trying to get all the clients that have doctors associated BUT none of them has started their first session (one client has_many doctors and can have first sessions with each of them).

So far I have:

@clients =  Client.joins(:doctors).where('doctors.first_session IS NULL').order('clients.id DESC')

But this doesn't work when a client has for example 2 doctors. the first doctor.first_session = null but the second one is not. This case will return the client and it don't want it to.

Any ideas?

4 Answers
Related