This project is archived and is in readonly mode.

#5300 ✓stale
Álvaro Bautista

ActiveRecord before_save callback not working with update_attributes method

Reported by Álvaro Bautista | August 4th, 2010 @ 08:22 PM

Writing some specs for a before_save callback it was not being called when I used the update_attributes method in rails 3.0.0.rc.

I attach a patch that can be applied to a fresh rails app (I used rails new lazytest -d sqlite3), I don't know if there is a proper way to show the error.

Within the patch is the Gemfile.lock where every gem version can be checked.

I used sqlite3 to create the patch but I found the error in an application that is using mysql.

Comments and changes to this ticket

  • Álvaro Bautista

    Álvaro Bautista August 4th, 2010 @ 08:34 PM

    I'm sorry, the patch I submitted was wrong.

    I can't reproduce the error in a fresh application.

    The main difference between the fresh application from which I created the patch and the one is failing is that in the one that fails I'm using RSpec2 instead of TestUnit, I don't know if that may be related to the issue.

    What I actually have in my code is:

    before_save :copy_first_release_info
    def copy_first_release_info
      if original_release?
        self.first_release_editor = editor
        self.first_release_year = year
        self.first_release_country = country

    in my model, and

    it "should copy edition info into first release info before save if original release is set to true" do
      album = Album.create(:artist => "Lovin' Spoonful", :title => "Do you believe in magic",
        :editor => "Kama Sutra",
        :year => 1965,
        :country => "USA",
        :original_release => true
      album.first_release_editor.should == "Kama Sutra"
      album.first_release_year.should == 1965
      album.first_release_country.should == "USA"
      album.update_attribute(:country, "United States of America")
      album.first_release_country.should == "United States of America"

    in my spec. The last should fails and I can't see why, because if I change the update_attribute call by an assignation and a save call it works fine.

    Sorry again.

  • Álvaro Bautista

    Álvaro Bautista August 4th, 2010 @ 09:47 PM

    I found out my mistake. The method that fails is update_attribute. So my previous diff didn't fail because I was using update_attributes.

    I attach the right patch now.

  • Álvaro Bautista

    Álvaro Bautista August 7th, 2010 @ 06:33 PM

    After a quick look at the AR source code it seems that skipping callbacks in this method is intentional:

    # Updates a single attribute and saves the record.  
    # This is especially useful for boolean flags on existing records. Also note that
    # * validation is skipped
    # * No callbacks are invoked 
    # * updated_at/updated_on column is updated if that column is available
    # * does not work on associations
    # * does not work on attr_accessor attributes. The attribute that is being updated must be column name.
  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 05:00 PM

    • State changed from “new” to “open”

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 05:00 PM

    • State changed from “open” to “stale”

Create your profile

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

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>

People watching this ticket