[ rails ] store : persist hash data in one table column

class Whatever < ActiveRecord::Base

store :added_config,
 accessors: [:param1, :param2, :param3],
 coder: YAML

NOTE : You can set custom coder to encode/decode your serialized attributes to/from different formats. JSON, YAML, Marshal.

Rails console

whatever = Whatever.last
whatever.added_config = {"param1"=>"test"}
whatever.save

whatevers table will contains column `added_config`

Accessor

We can also access with

whatever = Whatever.last

whatever.param1

ref : http://api.rubyonrails.org/classes/ActiveRecord/Store.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s