Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3643
Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3643
Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3651
Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3651
Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3654
Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3654
Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 1925
Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 1925
Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 2290
Warning: implode() [function.implode]: Argument must be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3242
Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3265
Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3265
Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3306
Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3357
Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3357
Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3502
Warning: Invalid argument supplied for foreach() in /home/aetheric/public_html/blender/wp-content/plugins/devformatter/geshi/geshi.php on line 3502
On a particular, nameless, heterogenous project using ruby on rails, ant, and shell scripts, there was an instance where data was not being properly cleaned before going into the database (primarily due to user errors in property files which were used by ant to feed the database). As a result, the database was filled with extraneous spaces which were causing many issues. A perfect illustration of GIGO.
One problem was that there were multiple databases, each with their own data. By the time the issue was discovered, a fix was needed soonest, so the solution was less than elegant, but it does illustrate the use of callbacks in ActiveRecord:
| | | copy code | | ? |
| 01 | module DbParanoia |
| 02 | # This is a really stupid thing to have to back-fit; it is making |
| 03 | # sure, however, that the users enter good data. We ensure that |
| 04 | # strings are stripped on read and write. |
| 05 | |
| 06 | def trim_space |
| 07 | self.attributes.each do |attr| |
| 08 | attr.strip! if attr.instance_of? String |
| 09 | end |
| 10 | end |
| 11 | |
| 12 | alias_method :after_find, :trim_space |
| 13 | alias_method :before_save, :trim_space |
| 14 | end |
| 15 |
The above module is using the after_find and before_save callbacks to make sure that:
- Any spaces coming back from the database are trimmed.
- Any new records and/or updates will have the spaces trimmed.
You can use this method to perform pretty much any sort of validation or data manipulation of the data of an ActiveRecord class. Other callbacks include:
- after_create
- after_destroy
- after_save
- after_update
- after_validation
- after_validation_on_create
- after_validation_on_update
- before_create
- before_destroy
- before_save
- before_update
- before_validation
- before_validation_on_create
- before_validation_on_update
You can read more about ActiveRecord::Callbacks at http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html.

