mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #21550 from didacte/unscope-associations
ActiveRecord: use association's `unscope` when preloading
This commit is contained in:
commit
4a375a83de
3 changed files with 26 additions and 1 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue