diff options
author | Byron Bischoff <byronb@gmail.com> | 2014-10-22 11:39:47 -0700 |
---|---|---|
committer | Byron Bischoff <byronb@gmail.com> | 2014-10-22 11:39:47 -0700 |
commit | 0f3cefa445c6b43711b29f38257adcedede74baa (patch) | |
tree | 55fd8cdd4380f478f4697f3e2ef82c088e3d0850 /activerecord | |
parent | 19d698d2b45ca33d8709c1993e39d7d9df284e3d (diff) | |
download | rails-0f3cefa445c6b43711b29f38257adcedede74baa.tar.gz rails-0f3cefa445c6b43711b29f38257adcedede74baa.tar.bz2 rails-0f3cefa445c6b43711b29f38257adcedede74baa.zip |
copy reflection_scopes’s unscoped value when building scope for preloading, fixes #11036
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/associations/preloader/association.rb | 4 | ||||
-rw-r--r-- | activerecord/test/cases/associations/has_many_associations_test.rb | 8 |
2 files changed, 12 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations/preloader/association.rb b/activerecord/lib/active_record/associations/preloader/association.rb index c0639742be..2300435f91 100644 --- a/activerecord/lib/active_record/associations/preloader/association.rb +++ b/activerecord/lib/active_record/associations/preloader/association.rb @@ -136,6 +136,10 @@ module ActiveRecord preload_values = preload_scope.values preload_binds = preload_scope.bind_values + if values[:unscope] + scope.unscope! values[:unscope] + end + scope.where_values = Array(values[:where]) + Array(preload_values[:where]) scope.references_values = Array(values[:references]) + Array(preload_values[:references]) scope.bind_values = (reflection_binds + preload_binds) diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index e34b993029..fdbe411f86 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -1895,6 +1895,14 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal [bulb1, bulb2], car.all_bulbs.sort_by(&:id) end + test 'unscopes the default scope of associated model when used with include' do + car = Car.create! + bulb = Bulb.create! name: "other", car: car + + assert_equal bulb, Car.find(car.id).all_bulbs.first + assert_equal bulb, Car.includes(:all_bulbs).find(car.id).all_bulbs.first + end + test "raises RecordNotDestroyed when replaced child can't be destroyed" do car = Car.create! original_child = FailedBulb.create!(car: car) |