Monday, September 28, 2009

View and edit deleted records with acts_as_paranoid

We at intinno use acts_as_paranoid so as not to permanently remove any data from the mysql tables. This also helps in restoring deleted data, if required.

Situation:

In Naarad(our erp solution for coaching institutes) users add teaching logs, which is basically a log book of which teacher taught which subject from what to what time. However your clients employ teachers which come and go. So teachers get added and deleted all the time. But if the teachers are deleted, then the logs refering to them would throw an error , saying the record doesn't exist.

Solution:

If we dont find the record, then try find_with_deleted first. If find with deleted fails, then render nil or blank

Basically instead of using

I now used

and force_find is defined as


Situation:

Ok now I can view the deleted record. But the logs can also be edited. So now the deleted records shoould be edited. But editing a deleted record sounded wierd.

Solution:

But there was a catch. In my case, logs corresponded to dates. So if a record is deleted, then it makes sense to edit its log entries before it was deleted. So in my app subjects had teachers and i defined a new method



Now while assigning teachers to log, all the teachers present on the system at a particular day are fetched.

Note: The above method in the application helper has been extracted out into a plugin. Read the details here.

1 comment:

AJ said...

Instead of using

object.nil? ? "" : object.send(association_attribute)

you can use


object.send(association_attribute) rescue ""