Ruby on rails范围就像OR一样

时间:2016-01-16 22:09:33

标签: ruby-on-rails

如何使用OR将这两个范围合二为一。像其中任何一个都可以是真的。

scope :search_language, lambda {|y| where(["language LIKE ?", "#{y}"])}
scope :search_published, lambda {|y| where(["published LIKE ?", "#{y}"])}

1 个答案:

答案 0 :(得分:0)

您可以将两者合并为一个查询:

scope :language_or_published, lambda { |y| where('language like ? or published like ?', y, y) }

或更新形式的lambda(后Ruby1.9):

scope :language_or_published, ->(y){ where('language like ? or published like ?', y, y) }