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:
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
Gemfile
1
gem'activerecord-postgres-hstore'
config/application.rb
1
config.active_record.schema_format=:sql#As the schema for hstore can't be represented by ruby
Terminal
1
rails g hstore:setup # Run 'CREATE EXTENSION IF NOT EXISTS hstore'
Migration File
12
add_column:users,:interests,:hstoreexecute'CREATE INDEX users_interests ON users USING GIN(interests)'#Index on the hstore field
Termial
1
rake db:migrate
The basic setup is done, now we are looking into how to use hstore in Rails’ MVC framework:
Usage in Rails
app/models/user.rb
123
attr_accessible:interests#To make sure interests is not mass assignment protectedstore_accessor:interests,%w{male, female}#Put any default keys showing in html formsserialize:interests,ActiveRecord::Coders::Hstore
defupdate@user=User.find(params[:id])if@user.update_attributes(params[:user])ifparams[:interests].present?andparams[:interests][:new_key].present?@user.interests=@user.interests.merge(params[:interests][:new_key]=>true)#Add the new key to the interests@user.saveendrender"#{some_path_with_success_message}"elserender"#{some_path_with_fail_message}"endend
Yeah, that’s it. You could be more flexible by not fixing any keys for the hstore field.