Rails 4中has_many关系的多个条件

时间:2014-05-21 08:57:33

标签: ruby-on-rails activerecord

我正在更新传统的Rails 2.0应用程序,并发现此代码似乎不再适用于Rails 4 ......:

company.rb:

has_many :orders
has_many :pending_orders,
    :class_name => "Order",
    :conditions => "(orders.coworker_id IS NULL OR orders.delivery_time IS NULL) AND orders.finished_at IS NULL",
    :order => "orders.created_at ASC"

在这种情况下,文档不是很全面,因此我正在寻找将此代码更新为Rails 4语法的正确解决方案。这是我的尝试,但我不确定这是否是正确的解决方案:

has_many :pending_orders, -> { (where(orders: {coworker_id: nil}) || where(orders: {delivery_time: nil})) && where(orders: {finished_at: nil}) }, class_name: "Order"

1 个答案:

答案 0 :(得分:1)

如果您的尝试不起作用,请尝试这样做

has_many :pending_orders, -> { where(orders.coworker_id IS NULL OR orders.delivery_time IS NULL) And orders.finished_at IS NULL }, class_name: "Order"

<强> Source