Mark Locklear
Mark Locklear

Reputation: 5325

Rails 3 console having clause with two conditions

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

Answers (2)

SHS
SHS

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

Philip Hallstrom
Philip Hallstrom

Reputation: 19879

User.where.not(email: nil).having('COUNT(*) > 1').group(:email)

Upvotes: 1

Related Questions