Skip to content

Ruby Blender

  • Blog
  • About
  • Archives
  • Log in
 
Less
More
Trim
Untrim
« Older
Home
Loading
Newer »
Archive for the 'rails' Category
20Aug09 Database Paranoia — ActiveRecord Callbacks
rails ruby
1 Comment

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:

module DbParanoia
# This is a really stupid thing to have to back-fit; it is making
# sure, however, that the users enter good data. We ensure that
# strings are stripped on read and write.

def trim_space
self.attributes.each do |attr|
attr.strip! if attr.instance_of? String
end
end

alias_method :after_find, :trim_space
alias_method :before_save, :trim_space
end

 

The above module is using the after_find and before_save callbacks to make sure that:

  1. Any spaces coming back from the database are trimmed.
  2. 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.

Enhanced by Zemanta
 
Browse Archives »
  • administrivia (2)
  • configuration (1)
  • metaprogramming (1)
  • mini-saga (5)
  • programming (10)
  • rails (1)
  • ruby (15)
  • rubygems (2)
  • Uncategorized (1)
 

Recent Posts

  • Default values for Attributes
  • Arguments with Trollop
  • Database Paranoia — ActiveRecord Callbacks
  • Ruby Bindings and Scope
  • What’s #method_missing missing?

Search

Browse by Category

  • administrivia (2)
  • configuration (1)
  • metaprogramming (1)
  • mini-saga (5)
  • programming (10)
  • rails (1)
  • ruby (15)
  • rubygems (2)
  • Uncategorized (1)

Browse by Tag

  • ActiveRecord
  • binding
  • Command-line interface
  • gotcha
  • irb
  • irbc
  • Languages
  • Library
  • Local variable
  • metaprogramming
  • mini-saga
  • paradigm
  • philosophy
  • programming
  • rails
  • Recursion
  • ruby
  • rubygems
  • Ruby on Rails
  • scope
  • utilities

Browse by Month

  • September 2009 (1)
  • August 2009 (4)
  • April 2009 (1)
  • February 2009 (11)
 
 
  • Blog
  • About
  • Archives
  • Log in
 


Theme Design by Jay Kwong | Powered by WordPress and K2

 

Home Top Archives Entries FeedComments Feed