DEV Community

Discussion on: The Complexity of Active Record Transactions

Collapse
 
katafrakt profile image
Paweł Świątkowski

Great article, as usual!

The main use case behind this addition seems to be saving lots of records whose attributes didn't change, where each attempted update would execute empty BEGIN/COMMIT statements (even though no UPDATE was issued), which didn't perform well.

This kind of bothers me. Isn't it something a developer should take care of, knowing the tradeoffs of the framework? record.save if record.changed? doesn't sound like a workaround for me but a proper programming approach to the problem of saving many objects.

Collapse
 
janko profile image
Janko Marohnić

Thanks! Yes, that shouldn't be difficult to do, but I suppose Active Record wants to prevent you from running into that performance problem to begin with. Personally, with Active Record it wouldn't come to my mind to add if record.changed?, because I assume record.save will not do anything if no columns changed. But instead of making ActiveRecord::Base#save not open a transaction in this case, which would avoid that problem, it was somehow better to implement lazy transactions? 🤷‍♂️