diff options
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/relation/query_methods.rb | 19 | ||||
-rw-r--r-- | activerecord/lib/active_record/relation/where_clause.rb | 23 |
2 files changed, 25 insertions, 17 deletions
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index c21730e6ae..1568064e18 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -410,9 +410,8 @@ module ActiveRecord raise ArgumentError, "Hash arguments in .unscope(*args) must have :where as the key." end - Array(target_value).each do |val| - where_unscoping(val) - end + target_values = Array(target_value).map(&:to_s) + self.where_clause = where_clause.except(*target_values) end else raise ArgumentError, "Unrecognized scoping: #{args.inspect}. Use .unscope(where: :attribute_name) or .unscope(:order), for example." @@ -916,20 +915,6 @@ module ActiveRecord self.send(unscope_code, result) end - def where_unscoping(target_value) - target_value = target_value.to_s - - self.where_values = where_values.reject do |rel| - case rel - when Arel::Nodes::Between, Arel::Nodes::In, Arel::Nodes::NotIn, Arel::Nodes::Equality, Arel::Nodes::NotEqual, Arel::Nodes::LessThanOrEqual, Arel::Nodes::GreaterThanOrEqual - subrelation = (rel.left.kind_of?(Arel::Attributes::Attribute) ? rel.left : rel.right) - subrelation.name.to_s == target_value - end - end - - self.bind_values = bind_values.reject { |col,_| col.name == target_value } - end - def custom_join_ast(table, joins) joins = joins.reject(&:blank?) diff --git a/activerecord/lib/active_record/relation/where_clause.rb b/activerecord/lib/active_record/relation/where_clause.rb index f03f95df0b..ca9c7d19a7 100644 --- a/activerecord/lib/active_record/relation/where_clause.rb +++ b/activerecord/lib/active_record/relation/where_clause.rb @@ -24,6 +24,13 @@ module ActiveRecord ) end + def except(*columns) + WhereClause.new( + predicates_except(columns), + binds_except(columns), + ) + end + def ==(other) other.is_a?(WhereClause) && predicates == other.predicates && @@ -83,6 +90,22 @@ module ActiveRecord Arel::Nodes::Not.new(node) end end + + def predicates_except(columns) + predicates.reject do |node| + case node + when Arel::Nodes::Between, Arel::Nodes::In, Arel::Nodes::NotIn, Arel::Nodes::Equality, Arel::Nodes::NotEqual, Arel::Nodes::LessThanOrEqual, Arel::Nodes::GreaterThanOrEqual + subrelation = (node.left.kind_of?(Arel::Attributes::Attribute) ? node.left : node.right) + columns.include?(subrelation.name.to_s) + end + end + end + + def binds_except(columns) + binds.reject do |column, _| + columns.include?(column.name) + end + end end end end |