This project is archived and is in readonly mode.
ActiveRecord::Rollback does not nil all id attributes
-
porras
- Tag changed from 2.3.2, activerecord, nested, rollback to 2.3.2, activerecord, nested, rollback, testcase
I also hit this in our app. I uploaded a failing test which reproduces it.
-
calavera
Following the clues I just arrived to this method into active_record/transactions.rb:
# Reset id and @new_record if the transaction rolls back. def rollback_active_record_state! id_present = has_attribute?(self.class.primary_key) previous_id = id previous_new_record = new_record? yield rescue Exception @new_record = previous_new_record if id_present self.id = previous_id else @attributes.delete(self.class.primary_key) @attributes_cache.delete(self.class.primary_key) end raise endWhen I executes the test that @porras uploaded the condition has_attribute?(self.class.primary_key) is false so the method doesn't modify the id, but I don't understand why it's returning false.
I'm just thinking there is a worst problem in this method. What happen when a user doesn't follow the convention and changes the name of the primary key attribute? I suppose these lines will raise an error because the id method won't be defined:
line 206: previous_id = id line 212: self.id = previous_id -
porras
@calavera it's not related. AR wraps each save (and its validations and callbacks) in a transaction (which is nested if you already have one opened). That's the transaction being rolled back there, not the big one. That is: if you have a transaction with 5 saves and the last one fails, the 5 saves are rolled back, but only the object whose save failed gets its id fixed.
Anyway, your issue with hardcoding 'id' is completely true.
-
calavera
ok. I'm opening a new ticket so XD
-
rails
- Tag changed from 2.3.2, activerecord, nested, rollback, testcase to 232, activerecord, nested, rollback, testcase
- State changed from new to open
- Importance changed from to
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.
-
rails
- State changed from open to stale
