Peter Marklund

Peter Marklund's Home


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: