Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On destroying do not touch destroyed belongs to association. #13455

Merged
merged 1 commit into from
Dec 23, 2013

Conversation

pftg
Copy link
Contributor

@pftg pftg commented Dec 22, 2013

Fixes: #13445

invoice = Invoice.create!(line_items: [line_item])

# 2 queries with delete of invoce and line_time records
# and 1 query for update `update_at` column for invoice
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the extra comments are needed.

@pftg
Copy link
Contributor Author

pftg commented Dec 22, 2013

Thanks guys, updated!

@@ -1,3 +1,30 @@
* Do not raise `'can not touch on a new record object'` exception on destroying already destroyed
object with touch association causes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pftg I think you left the "causes" by mistake post editing, or maybe I am reading the sentence wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 good catch

@pftg
Copy link
Contributor Author

pftg commented Dec 22, 2013

/cc @rafaelfranca, @senny, @guilleiguaran

comment.destroy # => raised exception on touching destroyed record

#After
comment.destroy # => does not touch destroyed record
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix is quite obvious from your description, I think we can shrink the example code to the minimum:

# Given Comment has belongs_to :post, touch: true
comment.post.destroy
comment.destroy # no longer raises an error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, updated!

@pftg
Copy link
Contributor Author

pftg commented Dec 23, 2013

@senny thanks for review, updated!

line_item = LineItem.create!
invoice = Invoice.create!(line_items: [line_item])

assert_queries(1) { invoice.destroy }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to assert_queries here. this should be considered setup code.

@senny
Copy link
Member

senny commented Dec 23, 2013

@pftg can you push a rebased version, this does no longer apply.

@pftg
Copy link
Contributor Author

pftg commented Dec 23, 2013

@senny done!

senny added a commit that referenced this pull request Dec 23, 2013
…record

On destroying do not touch destroyed belongs to association.
@senny senny merged commit 32c8aee into rails:master Dec 23, 2013
@pftg pftg deleted the 13445_fix_touch_destroyed_record branch December 23, 2013 13:53
@pftg
Copy link
Contributor Author

pftg commented Dec 23, 2013

@senny thanks!

@senny
Copy link
Member

senny commented Dec 23, 2013

@pftg thank you for your work 💛

I looked into backporting but it turns out the exception was only added on master: e52ff80 for reference

@pftg
Copy link
Contributor Author

pftg commented Dec 23, 2013

@senny, I'm not clear what did you mean, but this bug appears for 4.0.2 and for 4-0-stable too. Exception was added in 4.0.0 https://github.com/rails/rails/blob/4-0-stable/activerecord/lib/active_record/persistence.rb#L428. this gists show that problem for 4.0.2.

One thing only, that for 4-0-stable my test is failed, because trying to run extra query instead of failing on exception, for now I do not get why persisted? is not working and there is no raising of exception (I assume that persisted? was fixed for master), elsewhere it fails and show that update query is redundant.

Should I add new PR for 4-0-stable with similar patch, which fix it?

@senny
Copy link
Member

senny commented Dec 23, 2013

basically the exception raising behavior is only on master. However you are right, that the unnecessary touch query is executed. I can backport a modofied version of this oatch with an updated changelog entry. No need for another PR.

@pftg
Copy link
Contributor Author

pftg commented Dec 24, 2013

@senny you are right. Have refactored test to make it raise exception:

  def test_belongs_to_with_touch_option_on_destroy_with_destroyed_parent
    invoice   = Invoice.create!
    line_item = LineItem.create!(invoice: invoice)
    invoice.destroy

    assert_queries(1) { line_item.destroy }
  end

senny added a commit that referenced this pull request Dec 24, 2013
…record

On destroying do not touch destroyed belongs to association.
Conflicts:
	activerecord/CHANGELOG.md
	activerecord/lib/active_record/associations/builder/belongs_to.rb

The code that raises an error when touching destroyed records is not on `4-0-stable`.
It's only on `master`: e52ff80

I modified the CHANGELOG entry to explain the bug rather the exception raising behavior.
I also had to modify the test-case to use the same instance. Otherwise `destroy` did
not result on a `destroyed?` association record.

/cc @pftg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Destroying already destroyed object with touch association causes "can not touch on a new record object" error
4 participants