Many people asked why Mac OSX, Rails and Heroku prefer PostgreSql to MySql. This post is talking about one of its outstanding features: hstore
One year ago, Ryan Bates’s post had introduced hstore, but there are a few updates for Rails 3.2. If you did not know what hstore is, this is its usage in Rails:
1
|
|
It’s easy to find there are json-like key/value pairs in the example above. It’s more awesome that the key it_is_my_littler_secret is dynamic, which can be customized by front end users. Let’s see how it could happen with Rails now:
Get Started
1
|
|
1
|
|
1
|
|
1 2 |
|
1
|
|
The basic setup is done, now we are looking into how to use hstore in Rails’ MVC framework:
Usage in Rails
1 2 3 |
|
1 2 3 4 5 6 7 8 9 10 |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Yeah, that’s it. You could be more flexible by not fixing any keys for the hstore field.
Querying hstore fields is not pain at all, here’s the full list of hstore operations
Wana comment or punch on the post? Tweet to @aquajach