Dynabix: An ActiveRecord Ruby gem for attribute serialization

Dy-na-bix, tasty serialization attribute accessors for ActiveRecord

Presenting Dynabix. Dynabix is an ActiveRecord 3.x RubyGem that facilitates attribute serialization via dynamically created read/write accessors.


Data serialization is a technique that can be used to persist data to the database without changing the schema when adding or removing attributes. A single text field can contain multiple attributes. Serialization is useful for one-off situations like voting polls or frequently changing on-line questionnaires. Dynabix uses ActiveRecord's serialize method under the hood.

UPDATE 6/26/12

ActiveRecord as of 3.2.1, as pointed out in the comments, has a very similar native method store. Dynabix differs from store by providing a declarative DSL for defining multiple stores (Ruby 1.9+), has separate read/write accessors, and stores to the database as HashWithIndifferentAccess. Unless you need one of these specific features, using the native 'store' method is recommended.


Dynabix's source is available under the MIT license here https://github.com/robertwahler/dynabix. The documentation is located on Rubydoc.info at http://rubydoc.info/gems/dynabix


Add a text column "metadata" to your model migration. This column will store all the attribute values defined by Dynabix.

    class AddMetadataToThings < ActiveRecord::Migration

      def change
        add_column :things, :metadata, :text


Add accessors to your model using the default column name ":metadata", specify the attributes in a separate step.

    class Thing < ActiveRecord::Base

      # full accessors
      metadata_accessor :breakfast_food, :wheat_products, :needs_milk

      # read-only accessor
      metadata_reader :friends_with_spoons

Specifying attributes for full attribute accessors in one step

    class Thing < ActiveRecord::Base
      has_metadata :metadata, :breakfast_food, :wheat_products, :needs_milk

Using the new accessors

    thing = Thing.new

    thing.breakfast_food = 'a wheat like cereal"

    # same thing, but using the metadata hash directly
    thing.metadata[:breakfast_food] = 'a wheat like cereal"

Ruby 1.9+

Dynabix under Ruby 1.9+ enables specifying multiple metadata columns on a model. You are not limited to using the static "metadata" column.

Add text columns "cows" and "chickens" to your "thing" model migration

    class AddMetadataToThings < ActiveRecord::Migration

      def change
        add_column :things, :cows, :text
        add_column :things, :chickens, :text


Specifying multiple metadata serializers to segregate like data into separate database columns (Ruby 1.9 only)

    class Thing < ActiveRecord::Base
      has_metadata :cows
      has_metadata :chickens, :tasty, :feather_count

      # read-only
      cows_reader :likes_milk, :hates_eggs

      # write-only
      cows_writer :no_wheat_products

      # extra full accessors for chickens
      chickens_accessor :color, :likes_eggs, :egg_count

Using the new accessors

    thing = Thing.new

    # cow stuff
    thing.no_wheat_products = true

    # chicken stuff
    thing.likes_eggs = true
    thing.egg_count = 12

    # using the metadata hash directly to read the data since
    # we only created a write accessor
    thing.cows[:no_wheat_products].should be_true

Runtime dependencies

  • Activerecord 3.x


Add Dynabix to your Gemfile

gem "dynabix"

Install the gem with Bundler

bundle install


Get the source

cd workspace

git clone https://github.com/robertwahler/dynabix.git

cd dynabix

Install the dependencies

bundle install

Run the specs

bundle exec rake spec

Autotest with Guard

bundle exec guard
article comments powered by Disqus

Copyright 1999-2013, GearheadForHire, LLC iconGearheadForHire, LLC
Site design by GearheadForHire, LLC | v2.3.0