Reputation: 5325
I looking for Users who might have multiple accounts. The following query from the console works:
User.find(:all, :group => :email, :having => "count(*) >1")
However, there are some accounts that have an email of 'nil' which I would like to exclude. I tried:
User.find(:all, :group => :email, :having => "count(*) >1 and email is not nil")
...without success.
Upvotes: 1
Views: 575
Reputation: 7744
To add conditions, append a "where" clause right after your model class.
User.where("email IS NOT NULL").having(conditions).group(column)
Upvotes: 1
Reputation: 19879
User.where.not(email: nil).having('COUNT(*) > 1').group(:email)
Upvotes: 1