Reputation: 20558
I am developing an API in Rails 3. Lately I have seen some user accounts being double. I am not sure how many so I need a way to find out which accounts that are double.
Is there a way, in ruby, to search the entire database and fetch those user accounts that got the same email address (hence double)?
Thankful for all input!
Upvotes: 0
Views: 111
Reputation: 4809
Just open the Rails console (rails c
) and type something like this:
Account.group(:email).having('count_all > 1').count
This will return a Hash with email addresses being the key and the number of times it occured as the value. The result will look something like this:
=> #<OrderedHash {"[email protected]"=>2, "[email protected]"=>2}>
Then, I guess you could take those email addresses and actually get the accounts:
Account.where(:email => "[email protected]")
To output them all in the console, you could combine both of those like this:
email_hash = Account.group(:email).having('count_all > 1').count
email_hash.each do |email, count|
Account.where(:email => email).each do |account|
p account
end
end
Upvotes: 1
Reputation: 4113
I think if you try to use(for example):
UserAccount.all.group_by(&:email_address).each do |email, record|
#you will get a set of records grouped by email address
end
this will help you (You did not write detailed description of your models but if think you will get the clue)
Upvotes: 0