This project is archived and is in readonly mode.
ActiveRecord before_save callback not working with update_attributes method
-
Á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 endin 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" endin 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
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
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
- 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
- State changed from open to stale
