Rails ActiveRecord 条件 [英] Rails ActiveRecord conditions
本文介绍了Rails ActiveRecord 条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法创建这样的条件?
Is there a way to create a condition like this?
@products = Product.find(:all,
:limit => 5,
:conditions => { :products => { :locale => 'en', :id NOT '1' }, :tags => { :name => ['a','b']})
我想列出不包括产品 1 的所有产品.谢谢.
I would like to list all products not including product 1. Thx.
推荐答案
Rails 3
使用 squeel 宝石.
Product.where(
:products => { :locale => 'en', :id.not_in => '1' },
:tags => { :name => ['a','b']}
).limit(5)
Rails 2
为此使用 AR 扩展.它支持以下条件修饰符:
Use AR Extensions for this. It supports the following condition modifiers:
* _lt => less than
* _gt => greater than
* _lte => less than or equal to
* _gte => greater than or equal to
* _ne => not equal to
* _not => not equal to
现在您可以按如下方式重写您的查询:
Now you can rewrite your query as follows:
@products = Product.find(:all,
:limit => 5,
:joins => [:tags],
:conditions => { :locale => 'en', :id_not => '1', :tags => { :name => ['a','b']}
)
这篇关于Rails ActiveRecord 条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文