KEMBAR78
Ruby on Rails & PostgreSQL - v2 | PDF
Ruby-on-Rails

&
PostgreSQL
John Ashmead
Ruby-on-Rails
•

Based on Ruby

•

A Framework, like Django

•

Widely popular

•

Ruby + ActiveRecord + Lots of Web Stuff + Lots of
other Stuff

•

Michael Hardt: http://ruby.railstutorial.org
Ruby Version
Manager
•

RVM lets you run multiple versions of ruby

•

Copies everything into some trees, then switches
pointers around

•

Manages gems & gemsets

•

Bundle install, bundle update
Gemle
source 'https://rubygems.org'
!

ruby '2.0.0'
!

# Make sure we are using latest ‘rails’
gem 'rails', '4.0.0'
!

# Use Postgres as the database for Active Record
gem 'pg'
!

# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'
!

# switch to bootstrap, rspec, factory_girl, selenium,
capybara
!

…
ActiveRecord
•

Database adaptor

id

•

General purpose

name

•

Convention over
conguration

•

Reasonable defaults

•

Standalone product

maps

description
created_at
updated_at
Finally, PostgreSQL
•

‘pg’ gem

•

Provides ActiveRecord with
interface to Postgres

•

AR has an “execute”
function that lets you run
raw sql

•

‘postgres-pr’ gem gives
access to PostgreSQL
specic features, if needed
Foreign Keys
•

ActiveRecord has a lot of “associations”

•

Has_many, belongs_to, has_and_belongs_to_many

•

Doesn’t clean up the existing database

•

Doesn’t automatically generate the relevant keys

•

Doesn’t fix the metadata
create_table "maps", force: true do |t|
t.integer "user_id"
t.string
"map_type",
t.string
"name"
t.text
"description"
t.decimal "map_width"
t.decimal "map_height"
t.datetime "created_at"
t.datetime "updated_at"
end
CREATE TABLE maps (
id integer NOT NULL,
user_id integer,
map_type character varying(255),
name character varying(255),
description text,
map_width numeric,
map_height numeric,
created_at timestamp without time zone,
updated_at timestamp without time zone
);

Weirder
than
it
looks
Migrations
rails generate scaffold Map user_id:integer map_type:string
name:string description:text map_width:decimal
map_height:decimal

class CreateMaps < ActiveRecor
def change
create_table :maps do |t|
t.integer :user_id
t.string :map_type,
t.string :name,
t.text :description
t.decimal :map_width
t.decimal :map_height
!

t.timestamps
end
end
end
rake db:migrate && rails db:migrate RAILS_ENV=test
Heroku stack
•

Ruby-on-Rails front end

•

PostgreSQL preferred

•

Well documented

•

Free up to a reasonable point

•

Integrated with github

•

Lots of tools

•

Mostly works

•

https://www.heroku.com
Red

Green

Refactor

•

Agile & Stories

•

Examples & TDD (Testdriven development) over
APIs

•

MVC: Model/View/
Controller

•

Upgrades in RoR much
trickier than in PostgreSQL
Domain Specic Languages
•

•

PostgreSQL makes it
easy to hook existing
languages in: i.e.
Javascript as a server
language.

•

Gems versus packages.
About 30K gems! Of
widely varying quality.

•

YMMV

describe "home page" do

let!(:person) 
{ FactoryGirl.create(:person) }

}

Ruby makes it easy to
write DSLs

before {
sign_in person
visit home_people_path(person)
}
it { should have_content(person.name)
it { should have_title(person.name) }
end
Long learning curves
•

Ruby-on-rails has a
lot of levels

•

Achieving rapport
with rails takes time

•

Help: railscasts,
stackoverflow, …

•

Grow little neurons,
grow (damn you!)
There’s a page for that
•

http://localhost:8081/phpPgAdmin/

•

http://guides.rubyonrails.org

•

http://api.rubyonrails.org

•

https://codeclimate.com

•

http://localhost:3000

•

https://github.com

•

http://ruby.railstutorial.org/book/ruby-on-rails-tutorial?
version=4.0

•

http://ruby-doc.com/docs/ProgrammingRuby/

•

…

Ruby on Rails & PostgreSQL - v2

  • 1.
  • 2.
    Ruby-on-Rails • Based on Ruby • AFramework, like Django • Widely popular • Ruby + ActiveRecord + Lots of Web Stuff + Lots of other Stuff • Michael Hardt: http://ruby.railstutorial.org
  • 3.
    Ruby Version Manager • RVM letsyou run multiple versions of ruby • Copies everything into some trees, then switches pointers around • Manages gems & gemsets • Bundle install, bundle update
  • 4.
    Gemfile source 'https://rubygems.org' ! ruby '2.0.0' ! #Make sure we are using latest ‘rails’ gem 'rails', '4.0.0' ! # Use Postgres as the database for Active Record gem 'pg' ! # Use SCSS for stylesheets gem 'sass-rails', '~> 4.0.0' ! # switch to bootstrap, rspec, factory_girl, selenium, capybara ! …
  • 5.
    ActiveRecord • Database adaptor id • General purpose name • Conventionover configuration • Reasonable defaults • Standalone product maps description created_at updated_at
  • 6.
    Finally, PostgreSQL • ‘pg’ gem • ProvidesActiveRecord with interface to Postgres • AR has an “execute” function that lets you run raw sql • ‘postgres-pr’ gem gives access to PostgreSQL specific features, if needed
  • 7.
    Foreign Keys • ActiveRecord hasa lot of “associations” • Has_many, belongs_to, has_and_belongs_to_many • Doesn’t clean up the existing database • Doesn’t automatically generate the relevant keys • Doesn’t fix the metadata
  • 8.
    create_table "maps", force:true do |t| t.integer "user_id" t.string "map_type", t.string "name" t.text "description" t.decimal "map_width" t.decimal "map_height" t.datetime "created_at" t.datetime "updated_at" end CREATE TABLE maps ( id integer NOT NULL, user_id integer, map_type character varying(255), name character varying(255), description text, map_width numeric, map_height numeric, created_at timestamp without time zone, updated_at timestamp without time zone ); Weirder than it looks
  • 9.
    Migrations rails generate scaffoldMap user_id:integer map_type:string name:string description:text map_width:decimal map_height:decimal class CreateMaps < ActiveRecor def change create_table :maps do |t| t.integer :user_id t.string :map_type, t.string :name, t.text :description t.decimal :map_width t.decimal :map_height ! t.timestamps end end end rake db:migrate && rails db:migrate RAILS_ENV=test
  • 10.
    Heroku stack • Ruby-on-Rails frontend • PostgreSQL preferred • Well documented • Free up to a reasonable point • Integrated with github • Lots of tools • Mostly works • https://www.heroku.com
  • 11.
    Red Green Refactor • Agile & Stories • Examples& TDD (Testdriven development) over APIs • MVC: Model/View/ Controller • Upgrades in RoR much trickier than in PostgreSQL
  • 12.
    Domain Specific Languages • • PostgreSQLmakes it easy to hook existing languages in: i.e. Javascript as a server language. • Gems versus packages. About 30K gems! Of widely varying quality. • YMMV describe "home page" do let!(:person) { FactoryGirl.create(:person) } } Ruby makes it easy to write DSLs before { sign_in person visit home_people_path(person) } it { should have_content(person.name) it { should have_title(person.name) } end
  • 13.
    Long learning curves • Ruby-on-railshas a lot of levels • Achieving rapport with rails takes time • Help: railscasts, stackoverflow, … • Grow little neurons, grow (damn you!)
  • 14.
    There’s a pagefor that • http://localhost:8081/phpPgAdmin/ • http://guides.rubyonrails.org • http://api.rubyonrails.org • https://codeclimate.com • http://localhost:3000 • https://github.com • http://ruby.railstutorial.org/book/ruby-on-rails-tutorial? version=4.0 • http://ruby-doc.com/docs/ProgrammingRuby/ • …