Skip to content

tombenner/nested-hstore

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Nested Hstore

Store nested hashes and other types in ActiveRecord hstores

Overview

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
end

# 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'

Installation

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
end

Testing

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

License

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

About

Store nested hashes in hstores in ActiveRecord

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages