查找具有非零字段的所有记录?

时间:2010-09-29 19:49:33

标签: ruby-on-rails

我试图定义两个变量如下:

  1. @orders = Customer.find_all_by_order_date(nil)
  2. @nonorders = Customer.find_all_by_order_date(!nil)
  3. 第一个正常工作,但第二个没有。如何找到order_date字段不为零的客户?


    @nonorders = @customer.orders.find(:all, :conditions => "@customer.orders.order_date IS NOT NULL")
    

    给我以下错误:

    undefined method 'extract_options_from_args!' for ActiveRecord::Base:Class

    我已尝试更改条件,例如@orders.order_date@customer.order.order_date等。导致此错误的原因是什么?谢谢!

3 个答案:

答案 0 :(得分:21)

Rails 3:

@nonorders = Customers.where("customers.date IS NOT NULL")

Rails 2:

@nonorders = Customers.find(:all, :conditions => "customers.date IS NOT NULL")

答案 1 :(得分:1)

您传递的字符串:条件必须是sql片段。

在给出的示例中,“customers.date”指的是customers表的日期字段。我认为“客户”位并不是绝对必要的,因为表名在上下文中是清楚的。

在您的示例中,以下内容应该有效:

@nonorders = @customer.orders.find(:all, :conditions => "order_date IS NOT NULL")

答案 2 :(得分:0)

> Rails 3.x

@nonorders = Customers.where("date IS NOT NULL")


Unless you are doing some non rails conventional stuff,
there should be no need to specify the table name within
the SQL payload of the the where method