Conditionally nullify attributes.

Gem Version

About

Allows to conditionally (support both if and unless.) nullify attributes in before_validation hook.

Usage

class Post < ActiveRecord::Base
  # Store description as NULL when description is blank.
  nullify :description, unless: :description?
end

class Payment < ActiveRecord::Base
  # We don't want to store bank card vendor when payment method isn't bank card.
  nullify :bank_card_vendor, unless: :paid_using_bank_card?
end

Installing gem

Add to your Gemfile:

gem 'nullify-attr', '~> 1.0'