【问题标题】:Find all objects that have an associated object with a certain property查找具有具有特定属性的关联对象的所有对象
【发布时间】:2009-06-20 16:12:53
【问题描述】:

我有一个 Orderhas_many :shipments。我如何使用Order.find 来返回在某个时间(比如最后一小时)之后创建最新 货件的所有订单对象?

【问题讨论】:

    标签: sql ruby-on-rails activerecord join


    【解决方案1】:
    Order.find(
      :all,
      :joins => :shipments, 
      :select => 'distinct orders.*', 
      :conditions => ['shipments.created_at > ?', Time.now - 1.hour])
    

    :joins 确保您收到有发货的订单,:conditions 确保您只收到最近一小时内创建的发货。

    :select 表示您只能获得每个订单的一个实例,以防订单在过去一小时内有多个通知。

    我不确定“最新”规定是否重要,因为如果在过去一小时内创建了任何货​​件,那么最新货件也将符合该条件。

    【讨论】:

    • “我不确定‘最新’规定是否重要,因为如果在过去一小时内创建了任何货​​件,那么最新货件也将符合该条件。” -- 答:我是个白痴。
    猜你喜欢
    • 2014-11-04
    • 2015-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多