Active recored store, rails 3.2


Active Record Store allows us to declare hash keys accessors(inspired by mongoid document oriented data). Any accessor acts like any other attribute of a model. Basically it is used for storing additional attributes within a database record without the need to have to create additional attributes (columns in the database table) to store these values. The contents of these “additional attributes” are stored within the scope of an existing attribute and as serialized as a JSON object. Actually Active Record Store gives you a thin wrapper around serialize for the purpose of storing hashes in a single column. It’s like a simple key/value store backed into your record when you don’t care about being able to query that store outside the context of a single record.

You can then declare accessors to this store that are then accessible just like any other attribute of the model. This is very helpful for easily exposing store keys to a form or elsewhere that’s already built around just accessing attributes on the model.

Make sure that you declare the database column used for the serialized store as a text(not necessary), so there’s plenty of room.

take a look how it works…..

I have addressess table with 3 attributes name,phone and location. I want location as store

class CreateAddresses < ActiveRecord::Migration
  def change
    create_table :addresses do |t|
      t.string :name
      t.integer :phone
      t.string :location

      t.timestamps
    end
  end
end

Now we need to declare location as store within Address model with appropriate keys [ :country, :city, :state ]

class Address < ActiveRecord::Base
    store :location, accessors: [ :country, :city, :state ]
end

Now take a look from console how it initializing object and storing data.

1.9.2p318 :001 > Address.new
=> #<Address id: nil, name: nil, phone: nil, location: {}, created_at: nil, updated_at: nil>
As we can see there is an empty location hash initialized.

1.9.2p318 :002 > address = Address.new
 => #<Address id: nil, name: nil, phone: nil, location: {}, created_at: nil, updated_at: nil>
1.9.2p318 :004 > address.name = ‘Pravin’
 => “Pravin”
1.9.2p318 :006 > address.phone = ‘09749448334’
 => “09749448334”
1.9.2p318 :007 > address.country = ‘india’
 => “india”
1.9.2p318 :008 > address.city = ‘hyderabad’
 => “hyderabad”
1.9.2p318 :009 > address.state = ‘bihar’
 => “bihar”

Any attribute, even if not specified with an accessor

1.9.2p318 :011 > address[:zipcode] = ‘713370’
 => “713370”
1.9.2p318 :012 > address.save
   (0.3ms)  BEGIN
  SQL (60.6ms)  INSERT INTO `addresses` (`created_at`, `location`, `name`, `phone`, `updated_at`) VALUES (‘2012-07-07 02:49:48’, ‘—\n:country: india\n:city: hyderabad\n:state: bihar\n’, ‘Pravin Kumar’, 9749448334, ‘2012-07-07 02:49:48’)
   (102.3ms)  COMMIT
 => true
1.9.2p318 :013 > Address.first
  Address Load (1.3ms)  SELECT `addresses`.* FROM `addresses`
 => #<Address id: 5, name: “Pravin”, phone: 2147483647, location: {:country=>”india”, :city=>”hyderabad”, :state=>”bihar”, :zipcode=>”713370″}, created_at: “2012-07-07 02:55:41”, updated_at: “2012-07-07 02:55:41”>

Screenshot of view.

That’s it….!!!

Advertisements

2 thoughts on “Active recored store, rails 3.2

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