2012-01-13 69 views
2

Rails的反向范围

scope :delivered, where(:status => Status::DELIVERED) 
SELECT "orders".* FROM "orders" WHERE "orders"."status" IN ('Pending', 'Partially Prepared') 

现在我要添加新的范围,以获得所有除交付的订单。

scope :not_delivered, where(:status != Status::DELIVERED) 
SELECT "orders".* FROM "orders" WHERE ('t') 

如何获得反向范围?

回答

2

您必须使用字符串条件。 scope :not_delivered, where('status NOT ?', Status::DELIVERED)

对语法没有信心,我经常不做SQL。但你应该明白这个主意。

+1

应该是'scope:not_delivered,where('status!=?',Status :: DELIVERED)' – Egor 2012-12-20 10:12:49