diff options
author | Sean Griffin <sean@seantheprogrammer.com> | 2015-09-24 08:49:10 -0600 |
---|---|---|
committer | Sean Griffin <sean@seantheprogrammer.com> | 2015-09-24 08:50:22 -0600 |
commit | 4a375a83deab7a3089e718a1d18ddc4c2846cd40 (patch) | |
tree | ece41e5e5551fb21db2849dc1b80b6ba8a159219 /activerecord | |
parent | 3ae76f553f03b6233e7ec1520debac09bf13612e (diff) | |
parent | d25321b35328742ab97c08e7b1ed3cadeca5739b (diff) | |
download | rails-4a375a83deab7a3089e718a1d18ddc4c2846cd40.tar.gz rails-4a375a83deab7a3089e718a1d18ddc4c2846cd40.tar.bz2 rails-4a375a83deab7a3089e718a1d18ddc4c2846cd40.zip |
Merge pull request #21550 from didacte/unscope-associations
ActiveRecord: use association's `unscope` when preloading
Diffstat (limited to 'activerecord')
3 files changed, 26 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 98a5b27854..36fe3e5234 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,7 @@ +* Correctly apply `unscope` when preloading through associations. + + *Jimmy Bourassa* + * Fixed taking precision into count when assigning a value to timestamp attribute Timestamp column can have less precision than ruby timestamp diff --git a/activerecord/lib/active_record/associations/preloader/association.rb b/activerecord/lib/active_record/associations/preloader/association.rb index 1dc8bff193..92792a7a15 100644 --- a/activerecord/lib/active_record/associations/preloader/association.rb +++ b/activerecord/lib/active_record/associations/preloader/association.rb @@ -154,7 +154,7 @@ module ActiveRecord scope.where!(klass.table_name => { reflection.type => model.base_class.sti_name }) end - scope.unscope_values = Array(values[:unscope]) + scope.unscope_values = Array(values[:unscope]) + Array(preload_values[:unscope]) klass.default_scoped.merge(scope) end end diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb index d160c30375..20af436e02 100644 --- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb @@ -95,6 +95,15 @@ class DeveloperWithExtendOption < Developer has_and_belongs_to_many :projects, extend: NamedExtension end +class ProjectUnscopingDavidDefaultScope < ActiveRecord::Base + self.table_name = 'projects' + has_and_belongs_to_many :developers, -> { unscope(where: 'name') }, + class_name: "LazyBlockDeveloperCalledDavid", + join_table: "developers_projects", + foreign_key: "project_id", + association_foreign_key: "developer_id" +end + class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase fixtures :accounts, :companies, :categories, :posts, :categories_posts, :developers, :projects, :developers_projects, :parrots, :pirates, :parrots_pirates, :treasures, :price_estimates, :tags, :taggings, :computers @@ -936,4 +945,16 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase end assert_equal 1, professor.courses.count end + + def test_habtm_scope_can_unscope + project = ProjectUnscopingDavidDefaultScope.new + project.save! + + developer = LazyBlockDeveloperCalledDavid.new(name: "Not David") + developer.save! + project.developers << developer + + projects = ProjectUnscopingDavidDefaultScope.includes(:developers).where(id: project.id) + assert_equal 1, projects.first.developers.size + end end |