I’m getting a hash from an API call that returns values like the following:
[{"name"=>"Abby Allen", "id"=>"123"}, {"name"=>"Barry Burner", "id"=>"234"}, {"name"=>"Cat Catrelli", "id"=>"345"}, {"name"=>"Darrell Dogooder", "id"=>"456"}, {"name"=>"Eva Ewing", "id"=>"567"}]
I’d like to compare the id’s from this hash to the “apiid” column that I current have in my database (User model), and return all IDs that the hash and “apiid” column have in common.
i.e. my apiid column in my User model looks like this
apiid
001
123
125
333
345
I was trying it this way, but any suggestions can’t get it to work. Eventually, the apiid column will be long, so I’m looking for the most efficient way as well.
User.find_each(:select => "apiid") do |user|
@friendscommon = @friends.select{|key, hash| hash["id"] == user }
end
where @friends is the hash above.
Any suggestions would be appreciated. Thank you!
You want to extract an array of all the ids in the array of hashes:
This will cause apiids to be an array of the ids, ie:
And pass it into a query:
Here, the where clause effectively sees:
which translates to: