提交 4a375a83 编写于 作者: S Sean Griffin

Merge pull request #21550 from didacte/unscope-associations

ActiveRecord: use association's `unscope` when preloading
* 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 @@ def build_scope
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 @@ def category
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 @@ def test_alternate_database
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册