Transaction Script to Active Record
At their core, both the transaction script and active record patterns are based on the same principle: the business logic is implemented as a procedural script. The differ‐ ence between them is how the data structures are modeled: the active record pattern introduces the data structures to encapsulate the complexity of mapping them to the storage mechanism.
As a result, when working with data becomes challenging in a transaction script, refactor it into the active record pattern. Look for complicated data structures and encapsulate them in active record objects. Instead of accessing the database directly, use active records to abstract its model and structure.