Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
AutoDefaults sets the default values of nil attributes based on the database schema
Ruby
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
.gitignore
Gemfile
LICENSE
README.md
Rakefile
activerecord-auto_defaults.gemspec

README.md

Activerecord::AutoDefaults

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

Installation

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

Usage

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

  t.timestamps
end

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
end

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

Contributing

  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.