aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorByron Bischoff <byronb@gmail.com>2014-10-22 11:39:47 -0700
committerByron Bischoff <byronb@gmail.com>2014-10-22 11:39:47 -0700
commit0f3cefa445c6b43711b29f38257adcedede74baa (patch)
tree55fd8cdd4380f478f4697f3e2ef82c088e3d0850 /activerecord
parent19d698d2b45ca33d8709c1993e39d7d9df284e3d (diff)
downloadrails-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.rb4
-rw-r--r--activerecord/test/cases/associations/has_many_associations_test.rb8
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)