我正在使用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