Skip to content

Instantly share code, notes, and snippets.

@peter
Created November 25, 2010 15:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peter/715518 to your computer and use it in GitHub Desktop.
Save peter/715518 to your computer and use it in GitHub Desktop.
Make sure ActiveRecord saves NULL in your database instead of empty strings
module NullifyTextAttributes
def self.included(base)
base.class_eval do
before_validation :nullify_text_attributes
private
def nullify_text_attributes
self.class.columns.select { |c| [:text, :string].include?(c.type) }.each do |column|
send("#{column.name}=", nil) if read_attribute(column.name).blank?
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment