Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master

upgrade rails

latest commit 600e5ed5d5
Richard Schneeman schneems authored January 11, 2013
Octocat-spinner-32 app add GA :) March 14, 2012
Octocat-spinner-32 config add cache_control and set to serve static assets March 13, 2012
Octocat-spinner-32 db add index to data column March 08, 2012
Octocat-spinner-32 doc initial commit February 22, 2012
Octocat-spinner-32 lib initial commit February 22, 2012
Octocat-spinner-32 log initial commit February 22, 2012
Octocat-spinner-32 public devise import works February 22, 2012
Octocat-spinner-32 script initial commit February 22, 2012
Octocat-spinner-32 test initial hstore commit February 27, 2012
Octocat-spinner-32 vendor initial commit February 22, 2012
Octocat-spinner-32 .gitignore initial commit February 22, 2012
Octocat-spinner-32 Gemfile upgrade rails January 11, 2013
Octocat-spinner-32 Gemfile.lock upgrade rails January 11, 2013
Octocat-spinner-32 README.md update readme March 08, 2012
Octocat-spinner-32 Rakefile initial hstore commit February 27, 2012
Octocat-spinner-32 config.ru initial hstore commit February 27, 2012
README.md

Hstore Example App for Heroku

To view a working demo go to Heroku's Hstore Demo online or check out the Screencast.

Ensure you've got a recent version of Postgres (9.1 and up) by running psql --version. Upgrade if necessary. Clone this repo to your had drive. Move into that directory and run:

  gem install bundler
  bundle install

Then create a database and run your migrations

  bundle exec rake db:create
  bundle exec rake db:migrate

This migration will create Users, Products, and enable hstore in this database by executing this command in Postgres:

  CREATE EXTENSION hstore;

While the hstore column type is supported natively by Rails 4, it isn't in Rails 3 yet. Until Rails 4 is released we can use the activerecord-postgres-hstore gem. In your migration give the Products table a generic data column with a type of hstore.

  create_table :products do |t|
    t.string :name
    t.hstore :data
    t.timestamps
  end

Now We can open up a console and store any type of hash data to the data column of products like this:

    Product.create(:name => "Geek Love: A Novel", :data => {'author' => 'Katherine Dunn', 'pages' => 368, 'category' => 'fiction'})
    Product.last.data['category']  # => 'fiction'

As you see above, we can store any types of values that we want into the hstore. We can query the keys and values in any number of ways.

      # Find all products that have a key of 'author' in data
      Product.where("data ? :key", :key => 'author')

      # Find all products that have a 'pages' and '368' key value pair in data
      Product.where("data @> (:key => :value)", :key => 'pages', :value => '368')

      # Find all products that don't have a key value pair 'pages' and '999' in data
      Product.where("not data @> (:key => :value)", :key => 'pages', :value => '999')

      # Find all products having key 'author' and value like 'ba' in data
      Product.where("data -> :key LIKE :value",     :key => 'author, :value => "%Kat%")

Contact

Richard Schneeman @Schneems for Heroku.

licensed under MIT License Copyright (c) 2011 Schneems. See LICENSE.txt for further details.

Something went wrong with that request. Please try again.