【问题标题】:object with latest date mongoid具有最新日期的对象 mongoid
【发布时间】:2012-10-17 15:12:20
【问题描述】:

我有 2 个具有 created_at 属性的对象。

我想知道这两个对象中哪一个是最近日期的查询

对象 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。我需要使用最新日期。

非常感谢

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 mongoid


    【解决方案1】:

    我会说:

    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}
    

    【讨论】:

    • 谢谢我已经编辑了这个问题。对象在数组内。谢谢
    • 谢谢兄弟两个回复都是正确的:D。非常感谢!
    猜你喜欢
    • 2016-05-14
    • 2018-12-27
    • 2013-10-18
    • 1970-01-01
    • 1970-01-01
    • 2017-06-09
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多