Skip to content

Commit b381bff

Browse files
committed
Merge pull request rails#37598 from Edouard-chin/ec-where-not-relation-deprecated
Correctly deprecate where.not working as NOR for relations:
1 parent 81c52b1 commit b381bff

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

activerecord/lib/active_record/relation/query_methods.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ def not(opts, *rest)
6464

6565
private
6666
def not_behaves_as_nor?(opts)
67-
opts.is_a?(Hash) && opts.size > 1
67+
return false unless opts.is_a?(Hash)
68+
69+
opts.any? { |k, v| v.is_a?(Hash) && v.size > 1 } ||
70+
opts.size > 1
6871
end
6972
end
7073

activerecord/test/cases/relation/where_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,18 @@ def test_where_not_polymorphic_id_and_type_as_nor_is_deprecated
181181
assert_equal all - expected, only.sort_by(&:id).map(&:estimate_of)
182182
end
183183

184+
def test_where_not_association_as_nor_is_deprecated
185+
treasure = Treasure.create!(name: "my_treasure")
186+
PriceEstimate.create!(estimate_of: treasure, price: 2, currency: "USD")
187+
PriceEstimate.create(estimate_of: treasure, price: 2, currency: "EUR")
188+
189+
assert_deprecated do
190+
result = Treasure.joins(:price_estimates).where.not(price_estimates: { price: 2, currency: "USD" })
191+
192+
assert_predicate result, :empty?
193+
end
194+
end
195+
184196
def test_polymorphic_nested_array_where
185197
treasure = Treasure.new
186198
treasure.id = 1

activerecord/test/schema/schema.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,7 @@
746746
t.string :estimate_of_type
747747
t.integer :estimate_of_id
748748
t.integer :price
749+
t.string :currency
749750
end
750751

751752
create_table :products, force: true do |t|

0 commit comments

Comments
 (0)