Rails ActiveRecord 条件
有没有办法创造这样的条件?
@products = Product.find(:all,
:limit => 5,
:conditions => { :products => { :locale => 'en', :id NOT '1' }, :tags => { :name => ['a','b']})
我想列出除产品 1 之外的所有产品。 谢谢。
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']})
I would like to list all products not including product 1.
Thx.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Rails 3
使用 squeel gem。
Rails 2
使用AR 扩展 为此。它支持以下条件修饰符:
现在您可以按如下方式重写查询:
Rails 3
Use squeel gem.
Rails 2
Use AR Extensions for this. It supports the following condition modifiers:
Now you can rewrite your query as follows:
应该是这样的。原来的查询不太清楚,请根据您的需要进行调整。
It should be something like this. The original query wasn't really clear, adapt it to your needs.
另一种方法是使用 merge_conditions 将哈希条件转换为字符串。然后您可以添加任何您想要的内容,或者使用其他选项再次调用 merge_conditions 。
Another way is to use the merge_conditions which turns the hash conditions into a string. Then you can add on whatever you want or call merge_conditions again with other options.
Rails 3.2.9
控制器
模型
Rails 3.2.9
Controller
Model