Skip to content

Instantly share code, notes, and snippets.

@quanon
Last active August 29, 2015 14:22
Show Gist options
  • Save quanon/02ecc8aae7a38a98093e to your computer and use it in GitHub Desktop.
Save quanon/02ecc8aae7a38a98093e to your computer and use it in GitHub Desktop.
module Extension::ActiveRecordExtension
extend ActiveSupport::Concern
included do
# (参考)
# OR operator in WHERE clause with Arel in Rails 4.2
# http://stackoverflow.com/questions/27627390/or-operator-in-where-clause-with-arel-in-rails-4-2
#
# ActiveRecord::QueryMethods::WhereChain
# https://github.com/rails/rails/blob/v4.2.1/activerecord/lib/active_record/relation/query_methods.rb
ActiveRecord::QueryMethods::WhereChain.include(Module.new do
def or(*scopes)
bind_values = []
where_values = []
scopes.each do |scope|
temp_scope = scope.is_a?(Hash) ? @scope.model.where(scope) : scope
where_values << temp_scope.arel_table.grouping(temp_scope.where_values.inject(:and))
bind_values += temp_scope.bind_values
end
@scope.where_values += [where_values.inject(:or)]
@scope.bind_values += bind_values
@scope
end
end)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment