Using Postgres' hstore in Rails 4 with scopes

ruby, rails, postgres

Hstore has been around for a while now but its Rails support has become impressive since Rails 4. In addition to that, it’s now supported on Heroku Postgres.

Hstore allows you to store custom data against a row in the database. I’ve used it a little in the past but have often relied on Rails’ serializer to do the same thing. Hstore is an improvement on that, and I’ll show you why.

Hstore is an extension to Postgres which has to be enabled. This can be done through a migration.

The store_accessor tells rails what pseudo columns we want to use via the detail hstore column. As you can see validations can be used and do use them since there are no database restrictions in place for the hstore column.

The best thing about it all is that we can still query the hstore pseudo columns. In this example, we can find all the user who like cats by running User.like_cats. This is pretty awesome!

A few things to keep in mind:

You can only work with strings. So for dates, booleans, and others you'll want to put a bit of thought into how to manage those.

You might want to set up defaults for the columns. This will have to be done within your model.

Use validations to ensure data integrity. Don't use hstore for core fields though, use non-nullable columns.