public
Description: When Active Record objects are saved from a form, empty fields are saved as empty strings instead of nil. This kills most validations.
Homepage:
Clone URL: git://github.com/collectiveidea/clear_empty_attributes.git
Click here to lend your support to: clear_empty_attributes and make a donation at www.pledgie.com !
grosser (author)
Tue Nov 17 12:56:32 -0800 2009
brandon (committer)
Thu Nov 26 20:18:05 -0800 2009
commit  7c075e3137bfabff5f2a0eaf24fbf960528792b4
tree    f259819f9ddba84f91881564e6cabbb1ce2b5345
parent  6eb343fc1d9dbbaa2d9090c4d3844ade58de57d5
name age message
file .gitignore Tue Dec 23 04:23:28 -0800 2008 its a gem now too... [grosser]
file README.markdown Sun Nov 22 07:02:43 -0800 2009 gemcutter is default now hurray [grosser]
file Rakefile Thu Nov 26 20:18:05 -0800 2009 make spec to spectask, to work nicely with CI s... [grosser]
file VERSION Thu Nov 12 22:35:05 -0800 2009 Version bump to 0.2.0 [grosser]
file clear_empty_attributes.gemspec Thu Nov 12 22:46:16 -0800 2009 Regenerated gemspec for version 0.2.0 [grosser]
file init.rb Mon Feb 05 06:55:52 -0800 2007 oops [brandon]
directory lib/ Thu Nov 12 22:45:50 -0800 2009 switch to gemcutter for gem release [grosser]
directory spec/ Thu Nov 26 20:18:05 -0800 2009 make spec to spectask, to work nicely with CI s... [grosser]
directory tasks/ Thu Jan 15 22:15:35 -0800 2009 Rename rake task to db:clear_empty_attributes a... [brandon]
README.markdown

Problem

When AR objects are saved, empty fields are saved as '' instead of nil.

  • Complicates queries for empty fields (WHERE field IS NULL OR field = '')
  • Makes the use of unless field.blank? necessary (opposed to only if field)
  • Can lead to late-detected bugs because most of the time strings were filled or '' and suddenly they are nil
  • Some validations do not support :allow_blank=>true
  • Databases can handle NULL better & faster than empty strings (especially when using LIKE)

Solution

Defines an AR before_validation that sets empty Strings to nil.

Install

script/plugin install git://github.com/collectiveidea/clear_empty_attributes.git OR
sudo gem install clear_empty_attributes

Migration

When you are switching to clear_empty_attributes, run this task to remove any '' strings/texts from your database.

rake db:clear_empty_attributes

(only works when checked out or installed as plugin)

Authors

Hereby placed under public domain, do what you want, just do not hold anybody accountable...