If you're importing data from an external source and you end up getting a bunch of empty strings back, it's not a good idea to save empty strings in the database.
Here is why that is bad:
- You will confuse users who are trying to query the database. They will use phrases like 'where middle_name is null', but records with empty strings will not be returned by the query.
- You will probably end up with some null records that are created a different way. This will be confusing and tough to manage.
Fortunately, it's quite easy to make rails save those pesky empty strings as null values.
First, install the nilify_blanks gem:
Then, for any models for which you want to convert empty strings to null values, check and convert all fields:
class Post < ActiveRecord::Base