Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #6822 (closed defect: fixed)

Opened 3 years ago

Last modified 2 years ago

[PATCH] after_update callbacks should not unnecessarily load has_many association

Reported by: canadaduane Assigned to: technoweenie
Priority: normal Milestone: 2.x
Component: ActiveRecord Version:
Severity: normal Keywords:
Cc:

Description

Callbacks generated by add_multiple_associated_save_callbacks will currently load all associated data after the object is saved, even when the association has solely been used for write-only tasks. This results in a potentially expensive query where none is required.

For example:

author.posts.new(:title => "Merry Christmas")
author.save

In the background, the above query will unnecessarily perform this query as well:

SELECT * FROM posts WHERE author_id = 1

Test case included.

Attachments

has_many_after_update_callback_should_not_unnecessarily_load_association.diff (2.2 kB) - added by canadaduane on 12/11/06 22:36:51.
association_callbacks_fix.diff (4.4 kB) - added by stopdropandrew on 12/18/07 20:11:59.
New patch w/ additional tests

Change History

12/11/06 22:36:51 changed by canadaduane

  • attachment has_many_after_update_callback_should_not_unnecessarily_load_association.diff added.

12/18/07 20:11:59 changed by stopdropandrew

  • attachment association_callbacks_fix.diff added.

New patch w/ additional tests

12/18/07 20:13:26 changed by stopdropandrew

  • severity changed from minor to normal.

The supplied patch does not address associations that have new members to be saved. Here's a patch with a fix and additional tests.

12/27/07 03:48:33 changed by stopdropandrew

  • owner changed from core to technoweenie.

12/28/07 18:01:26 changed by rick

  • status changed from new to closed.
  • resolution set to fixed.

(In [8504]) Don't unnecessarily load has_many associations in after_update callbacks. Closes #6822 [stopdropandrew, canadaduane]