0

属性を持つ2つのオブジェクトがありcreated_atます。

これら2つのオブジェクトのどちらが最新の日付であるかについてのクエリを知りたい

オブジェクト1:

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

オブジェクト2:

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

編集済み

この2つのオブジェクトは配列内にあります:次のようなものです:

 [#<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>]

使えません.last。最新の日付を使用する必要があります。

どうもありがとうございます

4

1 に答える 1

3

私は言います:

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

または:

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

あなたの編集以来:

array.max_by{|u| u.created_at}
于 2012-10-17T15:15:59.607 に答える