Topic: Where to put action affecting multiple models

Hi

I have a billing business process which when complete needs to archive records from a number of different models. The actual billing process is done  through a model called Billable via  a method called geninvs i.e. Billable.geninvs.

I can create archive methods in each of the other models to allow them to archive themselves but I'm undecided as to where I should put the calls to these archive methods.  Gut feeling says it should be at the end of the Billable.geninvs method but is that the right way of doing things the 'Rails Way'?

All help gratefully appreciated.

Purvez

Re: Where to put action affecting multiple models

Two points:

1)  You could decouple the archiving of models from the Billable model with an observer.  I'd add an invoice log model,  and every time you succeed in generating your invoices,  write an entry into that log.  Then create an observer on that invoice log model,  and every time you see an insert,  fire off the archives. 

2)  If the archives take a long time,  you're stuck waiting for them to finish before you can run the geninvs again.  Say you generate invoices and notice you forgot to do something and a crucial invoice didn't get generated.  You'll have to wait until the archives finish before you can generate another invoice batch.  You could investigate https://github.com/ncr/background-fu to make the archives asynchronous.

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Where to put action affecting multiple models

Brad, thanks very much for that.  It does help when someone can articulate what is 'thrashing around' in my mind!!  I sort of instinctively knew that I had to keep the archiving away from the invoice generation.  I'm thinking since a new Invoice is created I might as well have an observer on that to trigger the  archiving, rather than create yet another model.  Is there anything 'wrong' about that approach?

Regarding asynch tasks, I've been reading up on various options with the main one being BackgroundRb but I had not come across background-fu.  Have you tried any of these and would you have a recommendation?

Thanks for your help
Purvez

Re: Where to put action affecting multiple models

I have not used either background-fu NOR BackgroundRb,  so nothing to recommend

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Where to put action affecting multiple models

>I'm thinking since a new Invoice is created I might as well have an observer on that to trigger the  archiving, rather than create yet another model.  Is there anything 'wrong' about that approach?

You'd fire of an archive on EVERY invoice,  do you want that????

Normally,  I process  invoices in batches,  and I'd only want all the tables archived after a BATCH of invoices is run.

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Where to put action affecting multiple models

I see your point about not wanting to archive after every invoice.  I had misunderstood your point about the invoice log model.  I thought you were suggesting logging the creation of every invoice but what you were suggesting was log the creation of the batch.  Thanks again for all your help.