ruby on rails - How can I ensure that calling #changed? on an instance of an ActiveRecord model after a transaction rolls back returns true? -


i found issue in code transaction block rollback explicitly raising activerecord::rollback. in case if instance of model saves in transaction before rollback ends in weird state.

here example of simple case causes issue:

record = somemodel.last record.attributes = { name: "a new name"}  somemodel.transaction    record.save   raise activerecord::rollback end 

if called record.changed? after transaction return false. however, if call record.name after transaction "a new name". reason in case record has changes not persisted in database, activerecord not identifying these changes anymore.

in real-world application causing issue in 1 area use transactions. have code if transaction fails attempt retry transaction again changed parameters. if reuse instance went through first transaction without reloading results in changes not being saved since don't appear changes.

does have suggestions how ensure instance has changes , activerecord still identifies these changes?

activerecord works way, because works way. can imagine scenarios in useful reload object database after rolling transaction. can imagine scenarios in not. like, maybe want preserve "new" fields log values our show them user.

you need work way works. since activerecord doesn't reload object after transaction rollback have yourself. suppose subclass code want.

as dirty behavior. says in docs dirty tracking behavior lost after calling save. transaction rollback doesn't preserve information seem inconsistent , annoying. perhaps rubyist can explain why has way, though makes sense way imagine code structured. activerecord has been around forever. wouldn't hold breath work differently in regard.


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -