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/test/cases | |
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/test/cases')
-rw-r--r-- | activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb | 21 |
1 files changed, 21 insertions, 0 deletions
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 |