I am developing a Rails 3 application (which acts as an API) and I allow people to send in a bunch of email address (v开发者_JS百科ia an iPhone app) and then automatically search the database for matching members (through emails).
It is working now (below code) but is returning doubles if the user sends in more than one copy of each email.
@users = @emails.find_all {|profile| User.find_by_email(profile['email']) }
I want the api to only return one unique copy of the user if found. I tried the below code but it does not work.
@users = @emails.find_all {|profile| User.select('DISTINCT email').where("LOWER (email) = ?", "%#{profile['email']}%") }
How can the first line of code above be changed to distinct lookups?
Thankful for all input!
I would take a look at
How do I get the unique elements from an array of hashes in Ruby?
a = [{:a => 1},{:a => 2}, {:a => 1}]
a.inject([]) { |result,h| result << h unless result.include?(h); result }
# this returns : [{:a=>1}, {:a=>2}]
Seems like to be what you need!
精彩评论