Skip to content

Instantly share code, notes, and snippets.

@sbl
Last active December 21, 2015 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbl/6381500 to your computer and use it in GitHub Desktop.
Save sbl/6381500 to your computer and use it in GitHub Desktop.
having fun with hstore as your document store
irb(main):001:0> p = Post.new
=> #<Post id: nil, type: "Post", data: nil, created_at: nil, updated_at: nil>
irb(main):002:0> p.valid?
=> false
irb(main):003:0> p.title = "a post about documents"
=> "a post about documents"
irb(main):004:0> p.valid?
=> true
irb(main):005:0> p.category = "worthless"
=> "worthless"
irb(main):006:0> p.save!
(0.2ms) BEGIN
SQL (4.5ms) INSERT INTO "documents" ("created_at", "data", "type", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["created_at", Thu, 29 Aug 2013 18:15:22 UTC +00:00], ["data", {"title"=>"a post about documents", "category"=>"worthless"}], ["type", "Post"], ["updated_at", Thu, 29 Aug 2013 18:15:22 UTC +00:00]]
(0.7ms) COMMIT
=> true
irb(main):007:0>
rails g model Document type:string data:hstore
class ActivateHstore < ActiveRecord::Migration
def self.up
execute "CREATE EXTENSION hstore"
end
def self.down
execute "DROP EXTENSION hstore"
end
end
class Post < Document
store_accessor :data, :title, :body, :category
validates_presence_of :title
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment