Last active
December 21, 2015 23:19
-
-
Save sbl/6381500 to your computer and use it in GitHub Desktop.
having fun with hstore as your document store
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rails g model Document type:string data:hstore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ActivateHstore < ActiveRecord::Migration | |
def self.up | |
execute "CREATE EXTENSION hstore" | |
end | |
def self.down | |
execute "DROP EXTENSION hstore" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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