mirror of https://github.com/rails/rails
Merge branch 'fix-21955'
This commit is contained in:
commit
2ba92bb809
|
@ -1,3 +1,9 @@
|
||||||
|
* Fix `rewhere` in a `has_many` association.
|
||||||
|
|
||||||
|
Fixes #21955.
|
||||||
|
|
||||||
|
*Josh Branchaud*, *Kal*
|
||||||
|
|
||||||
* `where` raises ArgumentError on unsupported types.
|
* `where` raises ArgumentError on unsupported types.
|
||||||
|
|
||||||
Fixes #20473.
|
Fixes #20473.
|
||||||
|
|
|
@ -147,9 +147,9 @@ module ActiveRecord
|
||||||
scope.includes! item.includes_values
|
scope.includes! item.includes_values
|
||||||
end
|
end
|
||||||
|
|
||||||
|
scope.unscope!(*item.unscope_values)
|
||||||
scope.where_clause += item.where_clause
|
scope.where_clause += item.where_clause
|
||||||
scope.order_values |= item.order_values
|
scope.order_values |= item.order_values
|
||||||
scope.unscope!(*item.unscope_values)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
reflection = reflection.next
|
reflection = reflection.next
|
||||||
|
|
|
@ -2181,6 +2181,26 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
|
||||||
assert_equal [bulb1, bulb2], car.all_bulbs.sort_by(&:id)
|
assert_equal [bulb1, bulb2], car.all_bulbs.sort_by(&:id)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "can unscope and where the default scope of the associated model" do
|
||||||
|
Car.has_many :other_bulbs, -> { unscope(where: [:name]).where(name: 'other') }, class_name: "Bulb"
|
||||||
|
car = Car.create!
|
||||||
|
bulb1 = Bulb.create! name: "defaulty", car: car
|
||||||
|
bulb2 = Bulb.create! name: "other", car: car
|
||||||
|
|
||||||
|
assert_equal [bulb1], car.bulbs
|
||||||
|
assert_equal [bulb2], car.other_bulbs
|
||||||
|
end
|
||||||
|
|
||||||
|
test "can rewhere the default scope of the associated model" do
|
||||||
|
Car.has_many :old_bulbs, -> { rewhere(name: 'old') }, class_name: "Bulb"
|
||||||
|
car = Car.create!
|
||||||
|
bulb1 = Bulb.create! name: "defaulty", car: car
|
||||||
|
bulb2 = Bulb.create! name: "old", car: car
|
||||||
|
|
||||||
|
assert_equal [bulb1], car.bulbs
|
||||||
|
assert_equal [bulb2], car.old_bulbs
|
||||||
|
end
|
||||||
|
|
||||||
test 'unscopes the default scope of associated model when used with include' do
|
test 'unscopes the default scope of associated model when used with include' do
|
||||||
car = Car.create!
|
car = Car.create!
|
||||||
bulb = Bulb.create! name: "other", car: car
|
bulb = Bulb.create! name: "other", car: car
|
||||||
|
|
Loading…
Reference in New Issue