Join GitHub today
Destroying already destroyed object with touch association causes "can not touch on a new record object" error #13445
The following test fails: https://gist.github.com/ronalchn/8078852
Trying to destroy an object which was already automatically destroyed by its parent causes the error.
class Post < ActiveRecord::Base has_many :comments, dependent: :destroy, inverse_of: :post # setting inverse does not change behaviour of tests end class Comment < ActiveRecord::Base belongs_to :post, touch: true, inverse_of: :comments end class BugTest < Minitest::Unit::TestCase def test_association_failing comment = Comment.create(post: Post.new) comment.post.destroy comment.destroy #=> ActiveRecord::ActiveRecordError: can not touch on a new record object end def test_comment_destroyed comment = Comment.create(post: Post.new) comment.post.destroy refute comment.persisted?, 'destroying post should destroy comment' #=> true end end
Normally, destroying an already destroyed object doesn't throw any errors, so this race condition should also not throw an error.
Also note the inconsistent behaviour that occurs when
comment.clear_association_cache comment.destroy #=> doesn't throw error anymore