Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #13455 from jetthoughts/13445_fix_touch_destroyed_…

…record

On destroying do not touch destroyed belongs to association.
  • Loading branch information...
commit 32c8aee04bfa53e985926976279505f7bc8f654f 2 parents 00fc64a + 2bcf715
Yves Senn senny authored
13 activerecord/CHANGELOG.md
View
@@ -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.
2  activerecord/lib/active_record/associations/builder/belongs_to.rb
View
@@ -112,7 +112,7 @@ def self.touch_record(o, foreign_key, name, touch) # :nodoc:
end
record = o.send name
- unless record.nil? || record.new_record?
+ if record && record.persisted?
if touch != true
record.touch touch
else
8 activerecord/test/cases/associations/belongs_to_associations_test.rb
View
@@ -356,6 +356,14 @@ def test_belongs_to_with_touch_option_on_destroy
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])
Please sign in to comment.
Something went wrong with that request. Please try again.