Permalink
Browse files

Merge pull request #23146 from piotrj/issue_18424

When deleting through records, take into account association conditions
  • Loading branch information...
kamipo committed Jan 10, 2018
2 parents f30f20c + eebcebd commit ae48c65e411e01c1045056562319666384bb1b63
@@ -1,3 +1,9 @@
* 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

0 comments on commit ae48c65

Please sign in to comment.