【发布时间】:2012-07-12 00:22:00
【问题描述】:
我正在使用 Ruby on Rails 3.2.2 和 Squeel gem。我想知道是否有一种方法可以使用多个范围方法,以便使用与这些范围方法 SQL 条件相关的OR 子句生成 SQL 查询。也就是说,我有:
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
是否可以使用范围方法(通过使用或不使用 Squeel gem)生成 SQL OR 子句?
【问题讨论】:
-
写一个带有OR sql语句的scope_method_3还不够吗?
-
@Trip - 我认为您正在考虑的
scope_method_3无法遵循 DRY(不要重复自己)原则。
标签: sql ruby-on-rails ruby ruby-on-rails-3 squeel