Rails 3.2 and Active Record Store

What exactly is Active Record Store and how to use it with Rails 3.2

Rails 3.2 was released earlier today and it provides a variety of new features including: Active Reload, Faster Dev Mode, Tagged Logger and, of coarse, Active Record Store. When Active Record store was released I was a bit curious as to how it works since I only saw the same example on every blog out there and the example itself just wasn’t doing it for me. Therefore this article will briefly explain how to use Active Record Store and how the contents of are stored in the database.

Last Updated

This article was first published on January 20th 2012 and was last updated on May 9th 2012.

Active Record Store

Active Record Store 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. Once fetched, they’re unserialized and used as regular attributes. Here’s the basic code for using Active Record Store:

# Class Definition
class User < ActiveRecord::Base

  # The schema structure of this record is:
  # String first_name
  # String last_name
  # Integer age
  # String details

  store :details, accessors: [ :height, :weight ]

end

# When Instantiated...
user = User.new
user.first_name = 'mati' # This is a regular attribute
user.last_name = 'pati'
user.age = 0
user.height = 20 # This is an ActiveReload attribute
user.width = 10
user.width # This is a getter method for the width attribute within the details value
user.details[:waist_length] = 100 # This is not a setter, but rather a hash key of the details hash (this will get serialized into the hash later regardless)

user.save

# Now if you look in the database then the storage value of the record will be as follows
first_name: "mati"
last_name: "pati"
age: 0
details: "{ height: '20', width: '10', waist_length: 100 }"

Active Record Store is a great idea for when you need to store optional data into a database record. One thing to keep in mind, however, is this does go against the 1st rule of normalization. This database storage method also makes database searching of the record unpractical when a search is issued on the storage column.

More about Rails 3.2

Some of the other things I noticed about 3.2 was that the development mode is alot faster. Rails startup and migration creation are much faster. Routes also load and parse faster. Other features such as Explain Queries, Compression of Page Cached Files and Tagged Logging are among the many new features provided by rails.

Some Street Cred

Here are some more links to follow up on Rails 3.2

2 Responses to “Rails 3.2 and Active Record Store”

  1. I gues you would want to make the “details” column of type TEXT so you can save some more data in it. Otherwise you would have to make sure you stay under 255 characters (for MySQL, should be similar for Postgres et al?)

  2. matsko:

    Hey Manuel. Keep in mind that MySQL VARCHAR columns are not limited to 255 chars. The default may be 255, but VARCHAR limits can go up to 65535.

    Postgres has a limit of 1GB for varchar columns.

Add Your Comment