Master detail logic

Can anyone share their practices in representing master detail applications from the controller/view perspective. I am more familiar with development environments that provide the logic of master detail solutions. My thought process is to use a wrapper class that contains one header class and an array of detail classes. I can also write the class methods to save and load both header and detail records from the database. But, when it comes to the logic of keeping track of CRUD activities of the detail records before saving to the database my thoughts become convoluted. And I advise you not to open that vault!

Let’s just use an oversimplified example of automobile maintenance records with the following:

Automobile Table
-ID (Auto Increment)

Maintenance Table
-ID (Auto Increment)
-AutoID (Foreign Key to the Automobile Table)

Now, the question is how do you keep track of the CRUD transactions of the maintenance table before saving to the database, of course using transactions, so that one can create, delete and update detail records in one transaction? Any advice and direction is greatly appreciated.

I think you’re on the right track.

We use ActiveRecord which encapsulates a lot the database work and is fairly Object Oriented and comes with a number of events that make this handy.

In AR we would have an Automobile object. It would have an array of Maintenance objects. When you call the generic Save method it would fire the AfterSave event which then allows us to iterate through the array and save any Maintenance objects not already saved. And since this happens in the event they’re all in the same transaction. So as far ‘keeping track’ of CRUD operations it’s pretty minimal since the parent object handles the save of the children objects.

When we load the Automobile record we don’t immediately load the Maintenance array. So we ‘lazy-load’ the array and do it only when the user asks for it. If you need data from both at the same time we’d recommend using a database view and let the db do the work of combining data because doing it in AR would be very inefficient with the lazy load (imagine a hundred cars each with a hundred maintenance records and there’s be 101 database queries to load it all).

There are a million different ways to dice and slice things but that’s the general gist of what we do with AR. If you want to play with AR you can download ARGen at

Thank you Bob. I will check out AR.