提交 ae48c65e 编写于 作者: R Ryuta Kamizono

Merge pull request #23146 from piotrj/issue_18424

When deleting through records, take into account association conditions
* Take into account association conditions when deleting through records.
Fixes #18424.
*Piotr Jakubowski*
* Fix nested `has_many :through` associations on unpersisted parent instances.
For example, if you have
......
......@@ -140,6 +140,7 @@ def delete_records(records, method)
scope = through_association.scope
scope.where! construct_join_attributes(*records)
scope = scope.where(through_scope_attributes)
case method
when :destroy
......
......@@ -1308,6 +1308,25 @@ def test_incorrectly_ordered_through_associations
end
end
def test_has_many_through_update_ids_with_conditions
author = Author.create!(name: "Bill")
category = categories(:general)
author.update(
special_categories_with_condition_ids: [category.id],
nonspecial_categories_with_condition_ids: [category.id]
)
assert_equal [category.id], author.special_categories_with_condition_ids
assert_equal [category.id], author.nonspecial_categories_with_condition_ids
author.update(nonspecial_categories_with_condition_ids: [])
author.reload
assert_equal [category.id], author.special_categories_with_condition_ids
assert_equal [], author.nonspecial_categories_with_condition_ids
end
def test_single_has_many_through_association_with_unpersisted_parent_instance
post_with_single_has_many_through = Class.new(Post) do
def self.name; "PostWithSingleHasManyThrough"; end
......
......@@ -88,6 +88,9 @@ def ratings
has_many :special_categories, through: :special_categorizations, source: :category
has_one :special_category, through: :special_categorizations, source: :category
has_many :special_categories_with_conditions, -> { where(categorizations: { special: true }) }, through: :categorizations, source: :category
has_many :nonspecial_categories_with_conditions, -> { where(categorizations: { special: false }) }, through: :categorizations, source: :category
has_many :categories_like_general, -> { where(name: "General") }, through: :categorizations, source: :category, class_name: "Category"
has_many :categorized_posts, through: :categorizations, source: :post
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册