Reputation: 12605
I have two tables with a has_one <--> belongs_to relationship: 'user' and 'account'. (An account must have a user, but a user need not have any accounts).
What I'd like to do is return all users who have no accounts...and I'm having a little trouble doing it gracefully. Is there a simple way to do this?
Many thanks...
Upvotes: 1
Views: 60
Reputation: 53319
You have to join the accounts table into the users table then check for an empty account. In Rails 3, you can do it like this:
User.includes(:account).where('accounts.id' => nil).all
In Rails 2, you can do it like this:
User.find(:all, :include => [ :account ], :conditions => { 'accounts.id' => nil })
Upvotes: 5