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.
previously: Shopping Cart #10