diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2018-01-11 07:18:35 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2018-01-11 07:31:05 +0900 |
commit | ae48c65e411e01c1045056562319666384bb1b63 (patch) | |
tree | 59560db534e4ea5ec17a3a6a00c3c2c0f8b8a3aa | |
parent | f30f20ccec9edbffc2b80b2d7e839a4fa9ac1eac (diff) | |
parent | eebcebdeb58ff7b6c05cb1cfbbc9aa4c85c9800e (diff) | |
download | rails-ae48c65e411e01c1045056562319666384bb1b63.tar.gz rails-ae48c65e411e01c1045056562319666384bb1b63.tar.bz2 rails-ae48c65e411e01c1045056562319666384bb1b63.zip |
Merge pull request #23146 from piotrj/issue_18424
When deleting through records, take into account association conditions
4 files changed, 29 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 3393534eb3..52c38edf81 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -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 diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb index ac86911ad8..7a3bef969b 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -140,6 +140,7 @@ module ActiveRecord scope = through_association.scope scope.where! construct_join_attributes(*records) + scope = scope.where(through_scope_attributes) case method when :destroy diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index 29de29ceb3..56a4b7c4d1 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -1308,6 +1308,25 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase 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 diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb index cb8686f315..27da886e1c 100644 --- a/activerecord/test/models/author.rb +++ b/activerecord/test/models/author.rb @@ -88,6 +88,9 @@ class Author < ActiveRecord::Base 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 |