Skip to content
Store nested hashes in hstores in ActiveRecord
Find file
Latest commit a26bea2 @tombenner Version 0.1.2

Nested Hstore

Store nested hashes and other types in ActiveRecord hstores


Postgres hstores offer a number of benefits, but they don't natively support multi-level data. Nested Hstore adds this support to ActiveRecord, letting you treat an hstore like a NoSQL-like document.

Hstore functions are still supported at the root level.

It also lets you store data types other than hashes in an hstore. All of the following values will be returned verbatim:

class User < ActiveRecord::Base
  serialize :my_property, ActiveRecord::Coders::NestedHstore

# Nested hash
user.my_property = {
  'name' => 'Jane Doe',
  'comment_ids' => [34, 67, 82],
  'location' => {
    'id' => 15,
    'city' => 'San Francisco',
    'state' => 'CA'

# Array
user.my_property = [34, 67, 82]

# Array of nested hashes
user.my_property = [
    'id' => 15,
    'username' => 'janedoe'
    'id' => 16,
    'username' => 'johndoe'

# Boolean
user.my_property = true

# Integer
user.my_property = 43

# Float
user.my_property = 43.1

# String
user.my_property = 'janedoe'


If you're using ActiveRecord 2.x or 3.x, set up activerecord-postgres-hstore if you haven't already. This isn't necessary for ActiveRecord 4.x.

Include it in your Gemfile:

gem 'nested-hstore'

Serialize each property using ActiveRecord::Coders::NestedHstore:

class User < ActiveRecord::Base
  serialize :my_property, ActiveRecord::Coders::NestedHstore


Nested Hstore is tested against ActiveRecord 3 and 4. If you'd like to submit a PR, please be sure to use Appraisal to test your changes in both contexts:

appraisal rspec


Nested Hstore is released under the MIT License. Please see the MIT-LICENSE file for details.

Something went wrong with that request. Please try again.