Build Status Gem

Use this gem for automatic behavior on attributes performed before validation. You can use the predefined extras or define your own.


Add this line to your application's Gemfile:

gem 'attribute_extras'

And then execute:

$ bundle

Or install it yourself as:

$ gem install attribute_extras


AttributeExtras provides three extras that are predefined: nullify_attributes, strip_attributes, and truncate_attributes. You can use these methods to tell AttributeExtras to perform mutations before validation. Additionally, you can call these methods at any time to perform the mutation programmatically. Examples are below:


Sets the value to nil if the value is blank.

class Person < ActiveRecord::Base
  nullify_attributes :name

person = Person.create(name: '   ') # => nil

person = '   ')
person.nullify_attributes # => nil


Strips the value.

class Person < ActiveRecord::Base
  strip_attributes :name

person = Person.create(name: '   value   ') # => 'value'

person = '   value   ')
person.strip_attributes # => 'value'


Truncates the value to the maximum length allowed by the column.

class Person < ActiveRecord::Base
  truncate_attributes :name

person = Person.create(name: 'a' * 500) # => 'a' * limit

person = 'a' * 500)
person.truncate_attributes # => 'a' * limit


You can define your own extras by using the define_extra method on the AttributeExtras module. define_extra takes a name for the extra and a block which itself accepts three arguments (the record being modified, the attribute being modified, and the value of the attribute). The block should return the modified value. An example would be:

AttributeExtras.define_extra :double_attributes do |_record, _attribute, value|
  value * 2

class Person < ActiveRecord::Base
  double_attributes :age

person = Person.create(age: 5)
person.age # => 10

person = 5)
person.age # => 10


After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to


Bug reports and pull requests are welcome on GitHub at


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