Skip to content


Subversion checkout URL

You can clone with
Download ZIP
AutoDefaults sets the default values of nil attributes based on the database schema
branch: master

Updating the README

latest commit f56b22ab42
Simon COURTOIS authored
Failed to load latest commit information.
lib Initial commit
.gitignore Initial commit
Gemfile Initial commit
LICENSE Initial commit Updating the README
Rakefile Initial commit
activerecord-auto_defaults.gemspec Initial commit


Sets the default values of nil attributes based on the database schema.


Add this line to your application's Gemfile:

gem 'activerecord-auto_defaults'

And then execute:

$ bundle

Or install it yourself as:

$ gem install activerecord-auto_defaults


Given the following code in your application schema:

create_table "articles", :force => true do |t|
  t.string  "title", :limit => 100,  :default => "", :null => false
  t.text    "body",  :limit => 3000, :default => "", :null => false


It may occur that you receive nil values for given attributes. Since they are flagged as NOT NULL and have de default value, it should be used to fill the nil attributes.

That's what AutoDefaults is for.

Simply add it to your model:

class Article < ActiveRecord::Base
  include ActiveRecord::AutoDefaults

And then try to create an article:

Article.create(title: nil, body: nil)

You will no longer get a SQL error telling you that a field should not be NULL and as a null value.

This avoids duplicating the schema instructions in a set_defaults callback to set this values before validation.

Add it to every ActiveRecord model

AutoDefaults provides a generator to use it on any AutoDefaults model.

rails generate auto_defaults:install
    create config/initializers/activerecord-auto_defaults.rb


  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request
Something went wrong with that request. Please try again.