0.0
No commit activity in last 3 years
No release in over 3 years
This gem allows to ensure there are no empty strings in DB, but nulls.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.15
~> 10.0
~> 3.0

Runtime

 Project Readme

NullifyEmpty

This gem allows to store string or text Rails model attributes as NULL in DB when they have empty values. It is very useful when you need to avoid "" values coming from blank form inputs or anywhere else to be stored in DB.

Having empty values in DB is not allowed when there is unique constraint on the column or uniqueness validation on the field, while presence of the values is not required. Unique constraint ignores nulls in most DBMS.

Installation

Add this line to your application's Gemfile:

gem 'nullify_empty'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install nullify_empty

Usage

Use nullify_empty method in the model to specify attributes that must not have empty values stored as empty strings in DB:

class User < ApplicationRecord
  nullify_empty :email, :phone
end
params = {email: "", phone: "", ...}
user = User.create(params) # => #<User id: 1, email: nil, phone: nil, ... > 

user.email = ""
user.email # => nil

user.update(email: "")
user.email # => nil

user.update_column :email, ""
user.email # => nil

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/chumakoff/nullify_empty. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.