diff options
author | Yves Senn <yves.senn@gmail.com> | 2014-11-05 15:03:35 +0100 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2014-11-05 15:13:23 +0100 |
commit | 8e9cb9787bab976fa3a9a7722c5a024dc8384de4 (patch) | |
tree | ca02ecb6123675b73c62993ea80acfc540199fda /activerecord | |
parent | 8d902a949b2ca6695766263756bdd274cf4988d8 (diff) | |
parent | 0f3cefa445c6b43711b29f38257adcedede74baa (diff) | |
download | rails-8e9cb9787bab976fa3a9a7722c5a024dc8384de4.tar.gz rails-8e9cb9787bab976fa3a9a7722c5a024dc8384de4.tar.bz2 rails-8e9cb9787bab976fa3a9a7722c5a024dc8384de4.zip |
Merge pull request #17360 from bronzle/includes_and_unscoped
copy reflection_scopes’s unscoped value when building scope for preloading
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG.md | 6 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/preloader/association.rb | 1 | ||||
-rw-r--r-- | activerecord/test/cases/associations/has_many_associations_test.rb | 8 |
3 files changed, 15 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 1961ba93c6..6bb1ea3cec 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,9 @@ +* Fix preloading of associations which unscope a default scope. + + Fixes #11036. + + *Byron Bischoff* + * Added SchemaDumper support for tables with jsonb columns. *Ted O'Meara* diff --git a/activerecord/lib/active_record/associations/preloader/association.rb b/activerecord/lib/active_record/associations/preloader/association.rb index c0639742be..496c426986 100644 --- a/activerecord/lib/active_record/associations/preloader/association.rb +++ b/activerecord/lib/active_record/associations/preloader/association.rb @@ -159,6 +159,7 @@ module ActiveRecord scope.where!(klass.table_name => { reflection.type => model.base_class.sti_name }) end + scope.unscope_values = Array(values[:unscope]) klass.default_scoped.merge(scope) end end 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) |