2

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
2
  • Not sure if I understand correctly. Is contact_messages_threads(@customer.id) or openmessagethread the array you want to find the newest record in? Commented Sep 28, 2015 at 13:31
  • @EgonWilzer contact_messages_threads is the method and I assume openmessagethread is the array. Commented Sep 28, 2015 at 13:33

1 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

2 Comments

For what? So openmessagethread is not the array? Can you try @messagelast = contact_messages_threads(@customer.id).max_by(&:message_date) ?
Got it. Thanks. Really confused matters. Thanks for clarifying. I thought contact_messages_threads was a method as I have defined it elsewhere.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.