Home

Awesome

Gem Version Build Status Coverage Status Dependency Status

SchemaPlus::DbDefault

SchemaPlus::DbDefault provides a constant ActiveRecord::DB_DEFAULT that you can use to set an attribute's database column to the default value or expression specified in the database schema.

(Without ActiveRecord::DB_DEFAULT you can update a column to NULL but not to its default value.)

SchemaPlus::DbDefault is part of the SchemaPlus family of Ruby on Rails ActiveRecord extension gems.

Usage

Use ActiveRecord::DB_DEFAULT as a "magic value" for attributes; when saved it will cause SQL to use the DB default value.

record.update_attributes(category: ActiveRecord::DB_DEFAULT) # SQL: UPDATE tablename SET category = DEFAULT
record.reload

After the save, you'll need to reload the record to replace ActiveRecord::DB_DEFAULT with the value assigned by the database.

Installation

<!-- SCHEMA_DEV: TEMPLATE INSTALLATION - begin --> <!-- These lines are auto-inserted from a schema_dev template -->

As usual:

gem "schema_plus_db_default"                # in a Gemfile
gem.add_dependency "schema_plus_db_default" # in a .gemspec
<!-- SCHEMA_DEV: TEMPLATE INSTALLATION - end -->

Compatibility

SchemaPlus::DbDefault is tested on:

<!-- SCHEMA_DEV: MATRIX - begin --> <!-- These lines are auto-generated by schema_dev based on schema_dev.yml --> <!-- SCHEMA_DEV: MATRIX - end -->

Sqlite3 does not support setting a column to its default value. Attempting to use ActiveRecord::DB_DEFAULTwill raise ActiveRecord::StatementInvalid

History

Development & Testing

Are you interested in contributing to SchemaPlus::DbDefault? Thanks! Please follow the standard protocol: fork, feature branch, develop, push, and issue pull request.

Some things to know about to help you develop and test:

<!-- SCHEMA_DEV: TEMPLATE USES SCHEMA_DEV - begin --> <!-- These lines are auto-inserted from a schema_dev template --> <!-- SCHEMA_DEV: TEMPLATE USES SCHEMA_DEV - end --> <!-- SCHEMA_DEV: TEMPLATE USES SCHEMA_PLUS_CORE - begin --> <!-- These lines are auto-inserted from a schema_dev template --> <!-- SCHEMA_DEV: TEMPLATE USES SCHEMA_PLUS_CORE - end --> <!-- SCHEMA_DEV: TEMPLATE USES SCHEMA_MONKEY - begin --> <!-- These lines are auto-inserted from a schema_dev template --> <!-- SCHEMA_DEV: TEMPLATE USES SCHEMA_MONKEY - end -->