user3385136
user3385136

Reputation: 553

How to return newest record in array

I want to return the most recent record in an array according to message_date. How would i go about this? I have this piece of code returning all and have tried using last etc. but I'm not too sure what i'm doing.

contact_messages_threads(@customer.id).each do |openmessagethread|
  - @messagelast = openmessagethread.message_date

Upvotes: 2

Views: 66

Answers (1)

IngoAlbers
IngoAlbers

Reputation: 5802

You should be able to get it like this if contact_messages_threads(@customer.id) returns the array:

@messagelast = contact_messages_threads(@customer.id).max_by(&:message_date)

Upvotes: 3

Related Questions