Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

ActiveRecord before_save callback not working with update_attributes method

#5300

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.

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

State: stale
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Álvaro Bautista
    Álvaro Bautista

    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
    
    private 
    
    def copy_first_release_info
      if original_release?
        self.first_release_editor = editor
        self.first_release_year = year
        self.first_release_country = country
      end
    end
    

    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.reload
      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.reload
      album.first_release_country.should == "United States of America"
    end
    

    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.

    August 4th, 2010 @ 08:34 PM

  2. Álvaro Bautista
    Álvaro Bautista

    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.

    August 4th, 2010 @ 09:47 PM

  3. Álvaro Bautista
    Álvaro Bautista

    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.
    

    August 7th, 2010 @ 06:33 PM

  4. Santiago Pastorino
    Santiago Pastorino
    • 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.

    February 2nd, 2011 @ 05:00 PM

  5. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 05:00 PM