aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorJimmy Bourassa <jimmy.bourassa@hooktstudios.com>2015-09-08 10:17:56 -0400
committerJimmy Bourassa <jimmy.bourassa@hooktstudios.com>2015-09-09 13:27:40 -0400
commitd25321b35328742ab97c08e7b1ed3cadeca5739b (patch)
tree991ccf1ca0840fa7aa238c10fb9600b84a5b0f5c /activerecord/test
parent269050072d53bc34d779c4f807c444aacb08a61a (diff)
downloadrails-d25321b35328742ab97c08e7b1ed3cadeca5739b.tar.gz
rails-d25321b35328742ab97c08e7b1ed3cadeca5739b.tar.bz2
rails-d25321b35328742ab97c08e7b1ed3cadeca5739b.zip
Include association's `unscope` when preloading
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb21
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