Rails ActiveRecord 条件

发布于 2024-08-22 17:59:56 字数 239 浏览 4 评论 0原文

有没有办法创造这样的条件?

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

霞映澄塘 2024-08-29 17:59:56

Rails 3

使用 squeel gem。

Product.where(
  :products => { :locale => 'en', :id.not_in => '1' }, 
  :tags => { :name => ['a','b']}
).limit(5)

Rails 2

使用AR 扩展 为此。它支持以下条件修饰符:

* _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

现在您可以按如下方式重写查询:

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

Rails 3

Use squeel gem.

Product.where(
  :products => { :locale => 'en', :id.not_in => '1' }, 
  :tags => { :name => ['a','b']}
).limit(5)

Rails 2

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']}
)
苍风燃霜 2024-08-29 17:59:56

应该是这样的。原来的查询不太清楚,请根据您的需要进行调整。

@products = Product.find(:all,
  :limit => 5,
  :conditions => ["locale = ? AND id <> ? AND tags.name IN (?)", "en", 1, ['a','b'],
  :joins => "tags"
)

It should be something like this. The original query wasn't really clear, adapt it to your needs.

@products = Product.find(:all,
  :limit => 5,
  :conditions => ["locale = ? AND id <> ? AND tags.name IN (?)", "en", 1, ['a','b'],
  :joins => "tags"
)
叶落知秋 2024-08-29 17:59:56

另一种方法是使用 merge_conditions 将哈希条件转换为字符串。然后您可以添加任何您想要的内容,或者使用其他选项再次调用 merge_conditions 。

hash_conditions = {:category => 'computers'}
conditions = Product.merge_conditions(hash_conditions) + ' AND products.id NOT IN(1139) '
products = Product.find(:all, :conditions => 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.

hash_conditions = {:category => 'computers'}
conditions = Product.merge_conditions(hash_conditions) + ' AND products.id NOT IN(1139) '
products = Product.find(:all, :conditions => conditions)
和我恋爱吧 2024-08-29 17:59:56

Rails 3.2.9


控制器

  @products = Product.english_but_not(1).with_tags('a','b').limit(5)

模型

class Product < ActiveRecord::Base
  attr_accessible :locale
  has_many :tags
  scope :english, -> { where(:locale => 'en') }
  scope :except_id, ->(id) { where(arel_table[:id].not_eq(id)) }
  scope :english_but_not, ->(id) { english.except_id(id) }
  scope :with_tags, ->(*names) { includes(:tags).where(:tags => {:name => names}) }
end

Rails 3.2.9


Controller

  @products = Product.english_but_not(1).with_tags('a','b').limit(5)

Model

class Product < ActiveRecord::Base
  attr_accessible :locale
  has_many :tags
  scope :english, -> { where(:locale => 'en') }
  scope :except_id, ->(id) { where(arel_table[:id].not_eq(id)) }
  scope :english_but_not, ->(id) { english.except_id(id) }
  scope :with_tags, ->(*names) { includes(:tags).where(:tags => {:name => names}) }
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文