ActiveRecord::Store typecasting & default values

ActiveRecord::Store is like a simple key/value store that you use for dynamic attributes in a relational databases, for examples storing settings without creating new cols.But to make it really useful, you need some sort of typecasting which from docs should be done like this:

class Song < ActiveRecord::Base

Uses a stored integer to hold the volume adjustment of the song

store :settings, accessors: [:volume_adjustment]

def volume_adjustment=(decibels)



def volume_adjustment




As you can see the idea that you should be overwriting the default accessors to type casting twice(once for getter & once for setter). so if you have 10attributes, you have to define 20methods in your model(verbose & not DRY). Further, this doesn’t handle default values. Hence, I propose following new syntax

store(:settings) do

boolean :allow_custom_url, default: false

integer :volume

integer :update_interval, default: 60

string :blurb

string :body_background, default: “light”

string :contrast, default: “red”


Feedback welcome

Hello Gaurish,

We use a simple and old module for this. It supports default values. First you do:

acts_as_having_settings field: ‘settings’

And then multiple times in multiple places (including monkey patches from other places):

settings_items :some_values, type: Array, default: [1,2,3]

settings_items :volume_on, type: :boolean, default: false

Source at: