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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<td><%= log.teacher.name %></td> |
I now used
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<td><%= force_find(log, "teacher", "name") %></td> |
and force_find is defined as
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def force_find(object, association, association_attribute) | |
class_name = association.camelize.constantize | |
id = object.send("#{association.underscore}_id") | |
object = class_name.find_with_deleted(id) | |
object.nil? ? "" : object.send(association_attribute) | |
end |
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def teachers_on(time) | |
condition = | |
(time && time.is_a?(Time) ? | |
"deleted_at is NULL OR deleted_at > '#{time.strftime("%Y-%m-%d %H:%M:%S")}' " : "") | |
self.teachers.find(:all, :with_deleted => true, :conditions => condition) | |
end |
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:
Instead of using
object.nil? ? "" : object.send(association_attribute)
you can use
object.send(association_attribute) rescue ""
Post a Comment