We talked about transactions in one of the previous articles (read it if you haven’t already), but we didn’t really touch on when to use them in real world. One of the most common use cases for me is when I just want to manage a single record while there are many changes happening on the page.
Adding a record to a transaction is simple
1 2 3 4 5 6 7 8 9 10
Now this is obvious, but what if we need to commit the transaction in a completely different action? Do we need to store the instance somewhere to use it later?
The answer is NO, we can always return the transaction in which the
record is by calling
.get("transaction"). We can even do it if we
decide to fetch the user again in a completely different part of the
It doesn’t matter in which part of the application you add the record to a transaction because you can always retrieve the correct instance later.
Which allows us to do something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Personally I use this when I only care about one record, but I know that
there might be other which are
dirty and I don’t want to commit those.
This happens almost every time you have two forms displayed at once.