0

I have a Contact model, and a User model, and a join table and each is HABTM more or less.

How can I query for the Contacts that have no users assigned to them? Driving me nuts.

Thanks

Phil
  • 133
  • 1
  • 8

2 Answers2

0

IMHO you should do a raw SQL query something along the lines of....

select c.*
from   contacts c left join contacts_users cu on c.id = cu.contact_id
where  cu.contact_id is null

I don't know of any pretty ORM-specific way to do it. Obviously you'll want to tailor the query to use the actual fields from your table.

MattC
  • 12,285
  • 10
  • 54
  • 78
0

I believe this thread is looking for the same thing right?

Want to find records with no associated records in Rails 3

If I understand you correctly, then I think it could be something like:

Contact.includes(:jointablenames).where( :jointablenames => {:contact_id => nil } )
Community
  • 1
  • 1
bknoles
  • 632
  • 7
  • 16