aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorYves Senn <yves.senn@gmail.com>2013-12-23 05:50:51 -0800
committerYves Senn <yves.senn@gmail.com>2013-12-23 05:50:51 -0800
commit32c8aee04bfa53e985926976279505f7bc8f654f (patch)
treec6ada73df4d56a65a9d4ddd5d94c9ae7a53d6333 /activerecord
parent00fc64a6dce4930e5f762c1c1d742addc0cf9e42 (diff)
parent2bcf7158d346e1b619aebbbec360ee0153ef8d06 (diff)
downloadrails-32c8aee04bfa53e985926976279505f7bc8f654f.tar.gz
rails-32c8aee04bfa53e985926976279505f7bc8f654f.tar.bz2
rails-32c8aee04bfa53e985926976279505f7bc8f654f.zip
Merge pull request #13455 from jetthoughts/13445_fix_touch_destroyed_record
On destroying do not touch destroyed belongs to association.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md13
-rw-r--r--activerecord/lib/active_record/associations/builder/belongs_to.rb2
-rw-r--r--activerecord/test/cases/associations/belongs_to_associations_test.rb8
3 files changed, 22 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 5ceb012aef..4e61a33fa1 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,16 @@
+* Do not raise `'can not touch on a new record object'` exception on destroying already destroyed
+ `belongs_to` association with `touch: true` option
+
+ Fixes: #13445
+
+ Example:
+
+ # Given Comment has belongs_to :post, touch: true
+ comment.post.destroy
+ comment.destroy # no longer raises an error
+
+ *Paul Nikitochkin*
+
* Fix a bug when assigning an array containing string numbers to a
PostgreSQL integer array column.
diff --git a/activerecord/lib/active_record/associations/builder/belongs_to.rb b/activerecord/lib/active_record/associations/builder/belongs_to.rb
index 62cc1e3a8d..5ccaa55a32 100644
--- a/activerecord/lib/active_record/associations/builder/belongs_to.rb
+++ b/activerecord/lib/active_record/associations/builder/belongs_to.rb
@@ -112,7 +112,7 @@ module ActiveRecord::Associations::Builder
end
record = o.send name
- unless record.nil? || record.new_record?
+ if record && record.persisted?
if touch != true
record.touch touch
else
diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb
index 6d01fcf50c..3205d0c28b 100644
--- a/activerecord/test/cases/associations/belongs_to_associations_test.rb
+++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb
@@ -356,6 +356,14 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
assert_queries(2) { line_item.destroy }
end
+ def test_belongs_to_with_touch_option_on_destroy_with_destroyed_parent
+ line_item = LineItem.create!
+ invoice = Invoice.create!(line_items: [line_item])
+ invoice.destroy
+
+ assert_queries(1) { line_item.destroy }
+ end
+
def test_belongs_to_with_touch_option_on_touch_and_reassigned_parent
line_item = LineItem.create!
Invoice.create!(line_items: [line_item])