hyperrjas
hyperrjas

Reputation: 10744

object with latest date mongoid

I have 2 objects with created_at attribute.

I want to know the query with Which of these two objects is the most recent date

object 1:

#<User _id: 504726081d41c809e5000003, _type: "User", created_at: 2012-09-05 10:14:33 UTC >

object 2:

#<User _id: 503fb40f1d41c8255a000007, _type: "User", created_at: 2012-08-30 18:42:24 UTC >

Edited

This 2 object are inside array: something like:

 [#<User _id: 504726081d41c809e5000003, _type: "User", created_at: 2012-09-05 10:14:33>, #<User _id: 503fb40f1d41c8255a000007, _type: "User", created_at: 2012-08-30 18:42:24>]

I can not use .last. I need use lastest date.

Thank you very much

Upvotes: 0

Views: 563

Answers (1)

apneadiving
apneadiving

Reputation: 115541

I'd say:

User.order_by([:created_at, :desc]).limit(1).first #useful to include other ordering conditions

Or:

User.desc(:created_at).limit(1).first

Since your edit:

array.max_by{|u| u.created_at}

Upvotes: 3

Related Questions