ConstantRecord

A tiny ActiveRecord substitute for small, never changing database tables.

*** Works with Rails 2 and Rails 3.0, but not with Rails 3.1 and above! ***

To get this gem working with the new versions of Rails (which means integration into the ARel mechanism), a complete rewrite would be necessary. Apologies, but please understand that I don’t find the time for this. Thanks!

Usage

class Currency < ConstantRecord::Base
  data 'EUR', 'USD', 'CAD', 'GBP', 'CHF'
end

or

class MoreDetailedCurrency < ConstantRecord::Base
  columns :name, :description
  data ['EUR', 'Euro'],
       ['USD', 'United States dollar'],
       ['CAD', 'Canadian dollar'],
       ['GBP', 'British pound sterling'],
       ['CHF', 'Swiss franc']
end

Inside ActiveRecord, it works just like a “normal” ActiveRecord association:

class Invoice < ActiveRecord::Base
  belongs_to :currency   # via currency_id database column
  validates_inclusion_of :currency_id, :in => Currency.ids

  def print_currency
    # Currency#name is the default attribute
    puts self.currency.name if self.currency
  end
end

ConstantRecord is meant as a substitute for ActiveRecord for tables, that will never change in the lifespan of a mongrel process. I.e. an application might let the user select a state of the US. Most applications will never have a feature where a user/admin can edit the list of US states. They are considered constant.

In my current application, my main/parent class has about 35 child classes, that use over 40 different constant classes. The use of ConstantRecord meant an estimated 15 to 20 per cent speed gain for my application.

ConstantRecord classes don’t need associations:

class CanadianProvince < ConstantRecord::Base
  data 'Alberta', ...
  has_many :people  # is not necessary, will not work
end

If you want to fetch all people living in Alberta, use:

Person.find(:all, :conditions =>
  {:province_id => CanadianProvince.find_by_name('Alberta').id}

or, in Rails 3:

Person.where(:province_id => CanadianProvince.find_by_name('Alberta').id)

If you ever think, ConstantRecord lacks key features of ActiveRecord, (submit a patch or) create the table and derive the class from ActiveRecord instead. This is a key design objective for ConstantRecord.

Features

Currency.find(1)              >>  #<Currency:0x332cbf4 @id=1, @name="EUR">

Just as in a database table, #id starts with 1. When data is a simple Array, #name is the default attribute.

MoreDetailedCurrency.find(3)  >>  #<MoreDetailedCurrency:0x2c8d94c 
                                    @description="Canadian Dollar", 
                                    @id=3, @name="CAD">

Currency.find_by_name('CHF')  >>  #<Currency:0x2c86ef8 @id=3, @name="CHF">

MoreDetailedCurrency.find_by_name('CAD')
                              >>  #<MoreDetailedCurrency:0x2c8d94c 
                                    @description="Canadian Dollar", 
                                    @id=3, @name="CAD">

Currency.count :all           >>  5

As a matter of fact, a ConstantRecord will often be used as a select field in an HTML form. Therefore it comes with a special feature, that is not ActiveRecord compatible. In a Form you can use:

<%= f.select :currency_id, Currency.options_for_select %>

or

<%= f.select :currency_id, Currency.options_for_select(:include_null => true) %>

If you want to store the name instead of the ID, then you can use:

<%= f.select :currency_id, Currency.options_for_select(:value => :name) %>

In this case, you must also change your validation:

class Invoice < ActiveRecord::Base
  validates_inclusion_of :currency_id, :in => Currency.names
end

Sample Data

This Gem features sample data for U.S. states, Canadian provinces and territories and German Bundesländer. Check out the sample directory!

Copyright © 2009 Christoph Petschnig. See LICENSE for details.

Thanks to Till Salzer, Torsten Schönebaum, Nate Wiger and Eric Lindvall for contributions!