Introducing ActiveHash, ActiveYaml and ActiveFile – easy readonly, file-based models

July 22, 2009 Pivotal Labs

ActiveHash is a simple base class that allows you to use a ruby hash as a readonly datasource for an ActiveRecord-like model.

ActiveHash assumes that every hash has an :id key, which is what you would probably store in a database. This allows you to seemlessly upgrade from ActiveHash objects to full ActiveRecord objects without having to change any code in your app, or any foreign keys in your database.

It also allows you to use #belongs_to in your AR objects.

ActiveHash can also be useful to create simple test classes that run without a database – ideal for testing plugins or gems that rely on simple AR behavior, but don’t want to deal with databases or migrations for the spec suite.

ActiveHash also ships with:

  • ActiveFile: a base class that will reload data from a flat file every time the flat file is changed
  • ActiveYaml: a base class that will turn YAML into a hash and load the data into an ActiveHash object


sudo gem install zilkey-active_hash

Or go to for more information.


To use ActiveHash, you need to:

  • Inherit from ActiveHash::Base
  • Define your fields
  • Define your data

A quick example would be:

class Country < ActiveHash::Base
  field :name = [
    {:id => 1, :name => "US"},
    {:id => 2, :name => "Canada"}

Defining Fields

You can define fields in 2 ways, using the :fields method, or using the :field method, which allows you to specify a default value for the field:

class Country < ActiveHash::Base
  fields  :name, :population
  field   :is_axis_of_evil, :default => false

Defining Data

You can define data inside your class or outside. For example, you might have a class like this:

# app/models/country.rb
class Country < ActiveHash::Base
  fields  :name, :population

# config/initializers/data.rb = [
    {:id => 1, :name => "US"},
    {:id => 2, :name => "Canada"}

If you prefer to store your data in YAML, see below.

Class Methods

ActiveHash gives you ActiveRecord-esque methods like:

Country.all             # => returns all Country objects
Country.count           # => returns the length of the .data array
Country.first           # => returns the first country object
Country.last            # => returns the last country object
Country.find 1          # => returns the first country object with that id
Country.find [1,2]      # => returns all Country objects with ids in the array
Country.find :all       # => same as .all
Country.find :all, args # => the second argument is totally ignored, but allows it to play nicely with AR
Country.find_by_id 1    # => find the first object that matches the id

It also gives you a few dynamic finder methods. For example, if you defined :name as a field, you’d get:

Country.find_by_name "foo"      # => returns the first object matching that name
Country.find_all_by_name "foo"  # => returns an array of the objects with matching names

Instance Methods

ActiveHash objects implement enough of the ActiveRecord api to satisfy most common needs. For example:

Country#id          # => returns the numeric id or nil
Country#quoted_id   # => returns the numeric id
Country#to_param    # => returns the id as a string
Country#new_record? # => false
Country#readonly?   # => true
Country#hash        # => the hash of the id (or the hash of nil)
Country#eql?        # => compares type and id, returns false if id is nil

ActiveHash also gives you methods related to the fields you defined. For example, if you defined :name as a field, you’d get:

Country#name        # => returns the passed in name
Country#name?       # => returns true if the name is not blank

Integration with Rails

You can create .belongs_to associations from rails objects, like so:

class Country < ActiveHash::Base
  fields  :name, :population

class Person < ActiveRecord::Base
  belongs_to :country

You can also use standard rails view helpers, like #collection_select:

<%= collection_select :person, :country_id, Country.all, :id, :name %>


If you want to store your data in YAML files, just inherit from ActiveYaml and specify your path information:

class Country < ActiveYaml::Base
  field :name

By default, this class will look for a yml file named “countries.yml” in the same directory as the file. You can either change the directory it looks in, the filename it looks for, or both:

class Country < ActiveYaml::Base
  set_root_path "/u/data"
  set_filename "sample"
  field :name

The above example will look for the file “/u/data/sample.yml”.

ActiveYaml, as well as ActiveFile, check the mtime of the file you specified, and reloads the data if the mtime has changed. So you can replace the data in the files even if your app is running in production mode in rails.


If you store encrypted data, or you’d like to store your flat files as CSV or XML or any other format, you can easily extend ActiveHash to parse and load your file. Just add a custom ::load_file method, and define the extension you want the file to use:

class Country < ActiveFile::Base
  set_root_path "/u/data"
  set_filename "sample"
  field :name

  class << self
    def extension

    def load_file

The two methods you need to implement are load_file, which needs to return an array of hashes, and .extension, which returns the file extension you are using. You have full_path available to you if you wish, or you can provide your own path.


Written by Mike Dalessio, Ben Woosley and Jeff Dean


About the Author


Take a Looksee at how a Ruby Object got its Methods
Take a Looksee at how a Ruby Object got its Methods

One property of the Ruby object model and object oriented programming in general is that a subclass of an o...

Sanitizing Solr requests
Sanitizing Solr requests

If you're accepting user input for Solr (which I expect most projects using it are), you've probably notice...


Subscribe to our Newsletter

Thank you!
Error - something went wrong!