Giter Site home page Giter Site logo

activerecord-define_nils's Introduction

Define Nils for ActiveRecord 3.x/4.x and Rails 3.x/4.x

If working with a legacy database used by another application that uses a non-null value to mean null, you may need to translate certain values to nil to make them more normal to work with in Ruby/Rails and then save nil as a specific value.

For example, if nil is stored as 0 in my_id:

define_nils as: 0, for: :my_id

If nil is stored as 0 in my_id and your_id:

define_nils as: 0, for: [:my_id, :your_id]

If you want to accept 0 or '' as nil for my_id but save nil as 0:

define_nils as: [0, ''], for: :my_id, saving_as: 0

It will choose the only/first element in the as if you don't specify saving_as, so a less clear way to define that is:

define_nils as: [0, ''], for: :my_id

If you want to accept 0 or '' as nil for my_id and your_id but save nil as nil/null:

define_nils as: [0, ''], for: [:my_id, :your_id], saving_as: nil

It handles read_attribute calls, calls to the attribute name, and all belongs_to associations defined after the define_nils that use the specified attribute as a foreign_key. It also will show the value as nil in calls to inspect/returned values in rails console. So, for the most part it should look as if the value is nil within Ruby but on the database-side, nil is stored as whatever value you want to represent nil.

Setup

In your ActiveRecord/Rails 3.1+ project, add this to your Gemfile:

gem 'activerecord-define_nils'

Then run:

bundle install

Usage

If you are on Rails, the concern ActiveRecordDefineNils::Model is included automatically in ActiveRecord::Base via railtie.

Be sure to place define_nils above your belongs_to associations in the model class or those associations will query on unaltered foreign key values, which is probably not what you want.

Authors

This was written by FineLine Prototyping, Inc. by the following contributors:

License

Copyright (c) 2013 FineLine Prototyping, Inc., released under the MIT license.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.