我正在使用Ruby on Rails 3.2.2和Squeel gem。我想知道是否有一种方法可以使用多个范围方法来生成与这些范围方法SQL条件相关的OR
子句的SQL查询。也就是说,我有:是否可以通过使用范围方法生成SQL`OR`子句?
class Article < ActiveRecord::Base
def self.scope_method_1
... # scope_method_1 SQL conditions
end
def self.scope_method_2
... # scope_method_2 SQL conditions
end
def self.scope_method_combination
# I am looking for something like
#
# where{scope_method_1 | scope_method_2} # Note: The '|' means the SQL 'OR' clause in the Squeel context
#
# so to generate the following SQL query:
#
# SELECT FROM articles WHERE <scope_method_1 SQL conditions> OR <scope_method_2 SQL conditions>
end
end
是否有可能生成SQL OR
条款与范围的方法(使用或不Squeel宝石)?
写一个scope_method_3写入OR sql语句会不够吗? – Trip 2012-07-12 00:34:34
@Trip - 我认为你正在考虑的'scope_method_3'不能遵循DRY(不要重复自己)的原则。 – Backo 2012-07-12 00:36:11