diff options
Diffstat (limited to 'guides/source/active_record_postgresql.md')
-rw-r--r-- | guides/source/active_record_postgresql.md | 33 |
1 files changed, 19 insertions, 14 deletions
diff --git a/guides/source/active_record_postgresql.md b/guides/source/active_record_postgresql.md index b69c8dbb7a..fa0f31cbbd 100644 --- a/guides/source/active_record_postgresql.md +++ b/guides/source/active_record_postgresql.md @@ -1,3 +1,5 @@ +**DO NOT READ THIS FILE IN GITHUB, GUIDES ARE PUBLISHED IN http://guides.rubyonrails.org.** + Active Record and PostgreSQL ============================ @@ -8,6 +10,7 @@ After reading this guide, you will know: * How to use PostgreSQL's datatypes. * How to use UUID primary keys. * How to implement full text search with PostgreSQL. +* How to back your Active Record models with database views. -------------------------------------------------------------------------------- @@ -51,11 +54,13 @@ Document.create payload: data ```ruby # db/migrate/20140207133952_create_books.rb -create_table :book do |t| +create_table :books do |t| t.string 'title' t.string 'tags', array: true t.integer 'ratings', array: true end +add_index :books, :tags, using: 'gin' +add_index :books, :ratings, using: 'gin' # app/models/book.rb class Book < ActiveRecord::Base @@ -80,9 +85,12 @@ Book.where("array_length(ratings, 1) >= 3") * [type definition](http://www.postgresql.org/docs/9.3/static/hstore.html) +NOTE: you need to enable the `hstore` extension to use hstore. + ```ruby # db/migrate/20131009135255_create_profiles.rb ActiveRecord::Schema.define do + enable_extension 'hstore' unless extension_enabled?('hstore') create_table :profiles do |t| t.hstore 'settings' end @@ -100,11 +108,6 @@ profile.settings # => {"color"=>"blue", "resolution"=>"800x600"} profile.settings = {"color" => "yellow", "resolution" => "1280x1024"} profile.save! - -## you need to call _will_change! if you are editing the store in place -profile.settings["color"] = "green" -profile.settings_will_change! -profile.save! ``` ### JSON @@ -129,7 +132,8 @@ event = Event.first event.payload # => {"kind"=>"user_renamed", "change"=>["jack", "john"]} ## Query based on JSON document -Event.where("payload->'kind' = ?", "user_renamed") +# The -> operator returns the original JSON type (which might be an object), whereas ->> returns text +Event.where("payload->>'kind' = ?", "user_renamed") ``` ### Range Types @@ -215,7 +219,7 @@ Currently there is no special support for enumerated types. They are mapped as normal text columns: ```ruby -# db/migrate/20131220144913_create_events.rb +# db/migrate/20131220144913_create_articles.rb execute <<-SQL CREATE TYPE article_status AS ENUM ('draft', 'published'); SQL @@ -277,7 +281,7 @@ end # Usage User.create settings: "01010011" user = User.first -user.settings # => "(Paris,Champs-Élysées)" +user.settings # => "01010011" user.settings = "0xAF" user.settings # => 10101111 user.save! @@ -322,7 +326,8 @@ macbook.address * [type definition](http://www.postgresql.org/docs/9.3/static/datatype-geometric.html) -All geometric types are mapped to normal text. +All geometric types, with the exception of `points` are mapped to normal text. +A point is casted to an array containing `x` and `y` coordinates. UUID Primary Keys @@ -370,8 +375,8 @@ Document.where("to_tsvector('english', title || ' ' || body) @@ to_tsquery(?)", "cat & dog") ``` -Views ------ +Database Views +-------------- * [view creation](http://www.postgresql.org/docs/9.3/static/sql-createview.html) @@ -426,8 +431,8 @@ second = Article.create! title: "Brace yourself", Article.count # => 1 first.archive! -p Article.count # => 2 +Article.count # => 2 ``` -Note: This application only cares about non-archived `Articles`. A view also +NOTE: This application only cares about non-archived `Articles`. A view also allows for conditions so we can exclude the archived `Articles` directly. |