2010-09-29 62 views
9

我试图定义两个变量如下:查找具有非零字段的所有记录?

  1. @orders = Customer.find_all_by_order_date(无)
  2. @nonorders = Customer.find_all_by_order_date(!无)

第一工作正常但第二个没有。如何找到那些order_date字段不为零的客户?


@nonorders = @ customer.orders.find(:全部:条件=> “@ customer.orders.order_date IS NOT NULL”)

是给我下面的错误:

未定义的方法`extract_options_from_args!'对于ActiveRecord :: Base:

我试过改变条件,比如@ orders.order_date,@ customer.order.order_date等。什么原因导致了这个错误?谢谢!

+0

在Rails中,通常用于模型类名首字母大写,单数。数据库表名通常是小写,复数。所以它应该是Customer.find(:all,:conditions =>“customers.date IS NOT NULL”)你应该检查这个问题。一个复数模型的名称,而不是通常的单数(客户,而不是客户)可以导致很多混乱,因为你建立你的SW。 – 2010-09-29 20:29:17

+0

复数客户不幸是一个错字 - 我在我的系统中使用客户。谢谢Larry! – sscirrus 2010-09-29 20:36:46

回答

21

Rails3中:

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

Rails2:

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

嗨Yannis,我上传了一个后续问题......不幸的是,仍然收到错误。 – sscirrus 2010-09-29 21:22:25

+1

或@nonorders = Customer.find(:all,:include =>:orders,:conditions => [“orders.order_date IS NOT NULL AND customers.id =?”,@ customer.id]) – Bohdan 2010-09-29 21:31:59

1

你为合格字符串:条件必须是一个SQL片段。

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

在您的例子下面应该工作:

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