Skip to content

Instantly share code, notes, and snippets.

@femoco
Created March 28, 2009 18:24
Show Gist options
  • Select an option

  • Save femoco/87166 to your computer and use it in GitHub Desktop.

Select an option

Save femoco/87166 to your computer and use it in GitHub Desktop.
# config/initializers/nullify_blank.rb
class ActiveRecord::Base
def self.nullify_blank(*args)
for arg in args
define_method :"#{arg.to_s}=" do |v|
v = nil if v.blank?
self[arg.to_sym] = v
end
end
end
end
# model
class User < ActiveRecord::Base
nullify_blank :author, :content
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment