#1115 new

After save callbacks called multiple times when dealing with has many through collections in the callback

Reported by Nathan | October 29th, 2009 @ 12:05 PM

This gist is a short ruby script that will exhibit the symptoms: http://gist.github.com/219844

The issue:

The after :save, :record_changes on Book is called one time after the #create.

The after :save, :record_changes on Book is called four times after the #save.

The difference is that in the create, changed_attrs is blank. In the save, it has a :title key. Since it has one key, a Change is created which causes after :save, :notify on Change to be called which calls #notification_of_change on Book, which adds to the publishers collection.

If you comment out lines 56 and 57, the lines that add to the publishers collection, you will see that the after :save, :record_changes on Book is now only called once after the #save.

It seems that adding to a has n :through collection causes #save to be called on Book, even when it's not dirty. This causes my after :save callback to be run three additional times (I don't know why it's three).

Comments and changes to this ticket

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

People watching this ticket