Search code examples
ruby-on-railspostgresqlruby-on-rails-4hstore

Rails and Postgres HSTORE Attribute Type


I have set up Postgres' hstore data type in my Rails 4 app and it works perfectly.

Is there a way to define default data type castings for each attribute in a Hstore column? This example explains:

{ "key1" => "1", "key2" => "2" }

I would like to retrieve "1" and "2" from this Model as INT by default. Is there any way to do this using Hstore or do I need to add ".to_i" every time?


Solution

  • You can use the gem hstore_accessor.

    The available types are: string, integer, float, decimal, datetime, date, boolean, array (deprecated), and hash (deprecated).