Created
November 25, 2010 15:15
-
-
Save peter/715518 to your computer and use it in GitHub Desktop.
Make sure ActiveRecord saves NULL in your database instead of empty strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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