Skip to content
This repository
Browse code

Merge pull request #12565 from stormsilver/unscope_fixes

Allow unscope to work with `where.not`
Conflicts:
	activerecord/CHANGELOG.md
  • Loading branch information...
commit efbdecc6f9a017fcbdcfca98df19b13bc98f1e68 1 parent beef637
Yves Senn senny authored
8 activerecord/CHANGELOG.md
Source Rendered
... ... @@ -1,5 +1,13 @@
1 1 ## unreleased ##
2 2
  3 +* `ActiveRecord::QueryMethods#unscope` unscopes negative equality
  4 +
  5 + Allows you to call `#unscope` on a relation with negative equality
  6 + operators, i.e. `Arel::Nodes::NotIn` and `Arel::Nodes::NotEqual` that have
  7 + been generated through the use of `where.not`.
  8 +
  9 + *Eric Hankins*
  10 +
3 11 * `NullRelation#pluck` takes a list of columns
4 12
5 13 The method signature in `NullRelation` was updated to mimic that in
2  activerecord/lib/active_record/relation/query_methods.rb
@@ -849,7 +849,7 @@ def where_unscoping(target_value)
849 849
850 850 where_values.reject! do |rel|
851 851 case rel
852   - when Arel::Nodes::In, Arel::Nodes::Equality
  852 + when Arel::Nodes::In, Arel::Nodes::NotIn, Arel::Nodes::Equality, Arel::Nodes::NotEqual
853 853 subrelation = (rel.left.kind_of?(Arel::Attributes::Attribute) ? rel.left : rel.right)
854 854 subrelation.name.to_sym == target_value_sym
855 855 else
20 activerecord/test/cases/scoping/default_scoping_test.rb
@@ -117,17 +117,25 @@ def test_unscope_after_reordering_and_combining
117 117 end
118 118
119 119 def test_unscope_with_where_attributes
120   - expected = Developer.order('salary DESC').collect { |dev| dev.name }
121   - received = DeveloperOrderedBySalary.where(name: 'David').unscope(where: :name).collect { |dev| dev.name }
  120 + expected = Developer.order('salary DESC').collect(&:name)
  121 + received = DeveloperOrderedBySalary.where(name: 'David').unscope(where: :name).collect(&:name)
122 122 assert_equal expected, received
123 123
124   - expected_2 = Developer.order('salary DESC').collect { |dev| dev.name }
125   - received_2 = DeveloperOrderedBySalary.select("id").where("name" => "Jamis").unscope({:where => :name}, :select).collect { |dev| dev.name }
  124 + expected_2 = Developer.order('salary DESC').collect(&:name)
  125 + received_2 = DeveloperOrderedBySalary.select("id").where("name" => "Jamis").unscope({:where => :name}, :select).collect(&:name)
126 126 assert_equal expected_2, received_2
127 127
128   - expected_3 = Developer.order('salary DESC').collect { |dev| dev.name }
129   - received_3 = DeveloperOrderedBySalary.select("id").where("name" => "Jamis").unscope(:select, :where).collect { |dev| dev.name }
  128 + expected_3 = Developer.order('salary DESC').collect(&:name)
  129 + received_3 = DeveloperOrderedBySalary.select("id").where("name" => "Jamis").unscope(:select, :where).collect(&:name)
130 130 assert_equal expected_3, received_3
  131 +
  132 + expected_4 = Developer.order('salary DESC').collect(&:name)
  133 + received_4 = DeveloperOrderedBySalary.where.not("name" => "Jamis").unscope(where: :name).collect(&:name)
  134 + assert_equal expected_4, received_4
  135 +
  136 + expected_5 = Developer.order('salary DESC').collect(&:name)
  137 + received_5 = DeveloperOrderedBySalary.where.not("name" => ["Jamis", "David"]).unscope(where: :name).collect(&:name)
  138 + assert_equal expected_5, received_5
131 139 end
132 140
133 141 def test_unscope_multiple_where_clauses

0 comments on commit efbdecc

Please sign in to comment.
Something went wrong with that request. Please try again.