Skip to content

Instantly share code, notes, and snippets.

@tylerhunt
Created January 5, 2011 13:54
Show Gist options
  • Save tylerhunt/766340 to your computer and use it in GitHub Desktop.
Save tylerhunt/766340 to your computer and use it in GitHub Desktop.
A Rails plugin for stripping strings on models and replacing empty strings with nil values.
module CoerceBlanks
def write_attribute(attr_name, value)
if value.is_a?(String)
value.strip!
value = nil if value.empty?
end
super
end
end
ActiveRecord::Base.class_eval do
include CoerceBlanks
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment