ruby-on-rails – Rails ActiveRecord条件

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – Rails ActiveRecord条件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有办法创建这样的条件?
  1. @products = Product.find(:all,:limit => 5,:conditions => { :products => { :locale => 'en',:id NOT '1' },:tags => { :name => ['a','b']})

我想列出所有不包括产品的产品1.
谢谢.

解决方法

轨道3

使用squeel宝石.

  1. Product.where(
  2. :products => { :locale => 'en',:id.not_in => '1' },'b']}
  3. ).limit(5)

轨道2

为此使用AR Extensions.它支持以下条件修饰符:

  1. * _lt => less than
  2. * _gt => greater than
  3. * _lte => less than or equal to
  4. * _gte => greater than or equal to
  5. * _ne => not equal to
  6. * _not => not equal to

现在可以重写您的查询如下:

  1. @products = Product.find(:all,:joins => [:tags],:conditions => { :locale => 'en',:id_not => '1','b']}
  2. )

猜你在找的Ruby相关文章