Repository is archived
No commit activity in last 3 years
No release in over 3 years
Sets the default values of nil attributes based on the database schema.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

 Project Readme

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