Peter Marklund

Peter Marklund's Home

Thu Nov 25 2010 15:32:03 GMT+0000 (Coordinated Universal Time)

Rails Tip: Inserting NULL to the Database Instead of Empty Strings

The value NULL in a relational database represents the absence of a value. Empty text fields and text areas in HTML forms on the other hand get submitted in Rails as empty strings. This means you can easily end up with empty strings in the database where you would expect NULL values. I came up with the following workaround for our ActiveRecord models: