Skip to content

Commit fc2b937

Browse files
authored
Merge pull request rails#47819 from Shopify/always-respect-explicitly-configured-query-constraints
Respect explicitly configured `query_constraints`
2 parents dc0f205 + 767cd52 commit fc2b937

File tree

4 files changed

+11
-2
lines changed

4 files changed

+11
-2
lines changed

activerecord/lib/active_record/persistence.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,12 +488,12 @@ def update!(id = :all, attributes)
488488
def query_constraints(*columns_list)
489489
raise ArgumentError, "You must specify at least one column to be used in querying" if columns_list.empty?
490490

491-
@_query_constraints_list = columns_list.map(&:to_s)
491+
@query_constraints_list = columns_list.map(&:to_s)
492492
end
493493

494494
def query_constraints_list # :nodoc:
495495
@query_constraints_list ||= if base_class? || primary_key != base_class.primary_key
496-
primary_key.is_a?(Array) ? primary_key : @_query_constraints_list
496+
primary_key if primary_key.is_a?(Array)
497497
else
498498
base_class.query_constraints_list
499499
end

activerecord/test/cases/persistence_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,4 +1522,8 @@ def test_query_constraints_raises_an_error_when_no_columns_provided
15221522
end
15231523
end
15241524
end
1525+
1526+
def test_child_class_with_query_constraints_overrides_parents
1527+
assert_equal(["clothing_type", "color", "size"], ClothingItem::Sized.query_constraints_list)
1528+
end
15251529
end

activerecord/test/models/clothing_item.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ class ClothingItem < ActiveRecord::Base
66

77
class ClothingItem::Used < ClothingItem
88
end
9+
10+
class ClothingItem::Sized < ClothingItem
11+
query_constraints :clothing_type, :color, :size
12+
end

activerecord/test/schema/schema.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@
260260
t.string :clothing_type
261261
t.string :color
262262
t.string :type
263+
t.string :size
263264
t.text :description
264265

265266
t.index [:clothing_type, :color], unique: true

0 commit comments

Comments
 (0)