I am not able to achieve the desired results with the conventional Ruby methods to remove all duplicate objects from the array, user_list, below. Is there a smart way to solve this problem?
users = []
user_list.each do |u|
user = User.find_by_id(u.user_id)
users << user
#users << user unless users.include?(user) # does not work
end
#users = users.uniq # does not work
How about this?
users = User.find(user_list.map(&:user_id).uniq)
This has the additional benefit of being one database call instead of user_list.size
database calls.
user_list.uniq!
This should remove all the duplicate value and keep the unique ones in user_list. I hope this is what you are looking for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With