I have several records with several attributes (A, B, C, D).
I want to be able to find which record has the higher value for a given attribute, such as D.
How do I do that?
You might give max_by a look.
objects = [some array of objects]
object_with_highest_value = objects.max_by {|obj| obj.desired_value }
Depending on how many records do you have, it can be more efficient to perform the search on the DB. I would order by the desired attribute descending, and take the first record:
User.order('field DESC').first
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