Connect a Ruby App to ReadySet¶
Once you have a ReadySet instance up and running, you connect your application to ReadySet exactly as you would to the upstream database.
This page gives you examples for a few common Postgres drivers and ORMS for Ruby.
Step 1. Start ReadySet¶
-
Install and start Docker Compose for your OS.
-
Download our Docker Compose and sample data files and start up Postgres and ReadySet locally:
curl -O "https://raw.githubusercontent.com/readysettech/docs/main/docs/assets/{docker-compose-postgres.yml,imdb-postgres.sql}"
This also imports two tables from the IMDb dataset that you'll query from your app.
Step 2. Get the code¶
-
Create a directory for the code and move into it:
-
Download the sample code:
curl -O "https://raw.githubusercontent.com/readysettech/docs/main/docs/assets/ruby-pg/{main.rb,Gemfile}"
Or create the following files:
#!ruby require 'pg' def query(conn) puts '------------------------------------------------' year = 1980 puts 'Year: %d' % [year] res = conn.exec('SELECT title_basics.originaltitle, title_ratings.averagerating FROM title_basics JOIN title_ratings ON title_basics.tconst = title_ratings.tconst WHERE title_basics.startyear = $1 AND title_basics.titletype = $2 AND title_ratings.numvotes > $3 ORDER BY title_ratings.averagerating DESC LIMIT 10', [year, 'movie', 50000]) res.each do |val| puts val end end def main() conn = PG.connect(ENV['DATABASE_URL']) query(conn) conn.close() end main()
-
Create a directory for the code and move into it:
-
Download the sample code:
curl -O "https://raw.githubusercontent.com/readysettech/docs/main/docs/assets/activerecord/{main.rb,Gemfile}"
Or create the following files:
require 'pg' require 'active_record' ActiveRecord::Base.establish_connection( adapter: 'postgresql', url: ENV['DATABASE_URL'] ) class Basic < ActiveRecord::Base self.table_name = 'title_basics' self.primary_key = 'tconst' end class Rating < ActiveRecord::Base self.table_name = 'title_ratings' self.primary_key = 'tconst' end year = 1980 puts 'Year: %d' % [year] Basic.find_by_sql(['SELECT title_basics.originaltitle, title_ratings.averagerating FROM title_basics JOIN title_ratings ON title_basics.tconst = title_ratings.tconst WHERE title_basics.startyear = ? AND title_basics.titletype = ? AND title_ratings.numvotes > ? ORDER BY title_ratings.averagerating DESC LIMIT 10', year, 'movie', 50000]). each { |r| puts "#{r.originaltitle}, #{r.averagerating}" }
Step 3. Install dependencies¶
-
Install
libpq
for your platform.For example, to install libpq on macOS with Homebrew, run the following command:
-
Configure
bundle
to uselibpq
.For example, if you installed
libpq
on macOS with Homebrew, run the following command:where
{libpq-path}
is the full path to thelibpq
installation on your machine (e.g.,/usr/local/opt/libpq
). -
Install the dependencies:
-
Install
libpq
for your platform.For example, to install libpq on macOS with Homebrew, run the following command:
-
Configure
bundle
to uselibpq
.For example, if you installed
libpq
on macOS with Homebrew, run the following command:where
{libpq-path}
is the full path to thelibpq
installation on your machine (e.g.,/usr/local/opt/libpq
). -
Install the dependencies:
Step 4. Connect and query¶
-
Set the
DATABASE_URL
environment variable to the connection string for ReadySet:Note
ReadySet takes the same standard-format connection string as Postgres.
In this case, since both ReadySet and Postgres are running locally, only the port portion is different (
5433
for ReadySet,5432
for Postgres). -
Run the code:
The code connects to ReadySet and then executes a query that joins results from two tables to get the title and average rating of the 10 top-rated movies with over 50,000 votes from 1980.
------------------------------------------------ Year: 1980 {"originaltitle"=>"The Empire Strikes Back", "averagerating"=>"8.7"} {"originaltitle"=>"The Shining", "averagerating"=>"8.4"} {"originaltitle"=>"Raging Bull", "averagerating"=>"8.2"} {"originaltitle"=>"The Elephant Man", "averagerating"=>"8.2"} {"originaltitle"=>"The Blues Brothers", "averagerating"=>"7.9"} {"originaltitle"=>"Airplane!", "averagerating"=>"7.7"} {"originaltitle"=>"Ordinary People", "averagerating"=>"7.7"} {"originaltitle"=>"The Gods Must Be Crazy", "averagerating"=>"7.3"} {"originaltitle"=>"Caddyshack", "averagerating"=>"7.2"} {"originaltitle"=>"Superman II", "averagerating"=>"6.8"}
Note
Since the query has not been cached in ReadySet, ReadySet proxies the query and returns the results from Postgres. After the query is cached, ReadySet returns the results directly and blazing fast!
-
Set the
DATABASE_URL
environment variable to the connection string for ReadySet:Note
ReadySet takes the same standard-format connection string as Postgres.
In this case, since both ReadySet and Postgres are running locally, only the port portion is different (
5433
for ReadySet,5432
for Postgres). -
Run the code:
The code connects to ReadySet and then executes a query that joins results from two tables to get the title and average rating of the 10 top-rated movies with over 50,000 votes from 1980.
Year: 1980 The Empire Strikes Back, 8.7 The Shining, 8.4 The Elephant Man, 8.2 Raging Bull, 8.2 The Blues Brothers, 7.9 Ordinary People, 7.7 Airplane!, 7.7 The Gods Must Be Crazy, 7.3 Caddyshack, 7.2 The Fog, 6.8
Note
Since the query has not been cached in ReadySet, ReadySet proxies the query and returns the results from Postgres. After the query is cached, ReadySet returns the results directly and blazing fast!