Remove leading and trailing whitespace from a model’s fields:

def before_validation
  attributes.each_key {|a| self[a].strip! if self[a].respond_to? :strip! }
end

I also found a more complicated version, implemented as an Observer, on the Rails wiki.

2 February 2006
rails