Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Postgresql + Ruby = :heart:

Postgresql + Ruby = :heart:

How you could use postgresql in ways you didn't imagine when using it as a simple datastore.

Yannick Schutz

April 08, 2014
Tweet

More Decks by Yannick Schutz

Other Decks in Programming

Transcript

  1. a) You add a new cache layer like Redis b)

    You add a table with all the fields c) You add fields to an existing model d) Something else?
  2. a) You add a new cache layer like Redis b)

    You add a table with all the fields c) You add fields to an existing model d) Something else?
  3. a) You add a new cache layer like Redis b)

    You add a table with all the fields c) You add fields to an existing model d) Something else?
  4. a) You add a new cache layer like Redis b)

    You add a table with all the fields c) You add fields to an existing model d) Something else?
  5. a) You add a new cache layer like Redis b)

    You add a table with all the fields c) You add fields to an existing model d) Something else?
  6. a) You store that in Redis b) You add a

    table with all the fields c) You add fields to the existing model d) Something else?
  7. a) You store that in Redis b) You add a

    table with all the fields c) You add fields to the existing model d) Something else?
  8. a) You store that in Redis b) You add a

    table with all the fields c) You add fields to the existing model d) Something else?
  9. a) You store that in Redis b) You add a

    table with all the fields c) You add fields to the existing model d) Something else?
  10. a) You store that in Redis b) You add a

    table with all the fields c) You add fields to the existing model d) Something else?
  11. add_column :users, :settings, :hstore ! class User < ActiveRecord::Base #

    This exposes accessors. user.wants_push = false store_accessor :settings, :wants_push, :wants_mails, :auto_save store_accessor :avatars, :large, :medium, :small end
  12. You have a really complex query that is blazing fast

    but when you come back a week after you’re like ‘WTF is that!’
  13. def bad_users_with_karma User.find_by_sql(query) end ! def query <<-SQL WITH bad_users

    AS (SELECT * FROM users WHERE bad = true) SELECT * FROM bad_users WHERE karma > #{karma_limit} SQL end
  14. CREATE MATERIALIZED VIEW bad_users AS SELECT * FROM users WHERE

    bad = false; ! class BadUser < User def readonly? true end end