Skip to content

Instantly share code, notes, and snippets.

@davidvandusen
Last active August 29, 2015 14:17
Show Gist options
  • Save davidvandusen/437e4c2fe276e3ae2daa to your computer and use it in GitHub Desktop.
Save davidvandusen/437e4c2fe276e3ae2daa to your computer and use it in GitHub Desktop.
ORM Lecture
CREATE TABLE instructors (
id SERIAL PRIMARY KEY NOT NULL,
name VARCHAR(100),
coolness INT DEFAULT 5
);
require 'pg'
class Instructor
# These are my database credentials. You'll need to replace them with yours.
CONN = PG::Connection.new({
host: 'localhost',
user: 'postgres',
password: 'postgres',
dbname: 'ormlecture'
})
attr_accessor :name, :coolness
attr_reader :id
def initialize(name, coolness, id=nil)
@name = name
@coolness = coolness
@id = id
end
def is_new?
@id.nil?
end
def valid?
@coolness > 9000
end
def save
raise 'Invalid Instructor!' unless valid?
if is_new?
result = CONN.exec_params('INSERT INTO instructors (name, coolness) VALUES ($1, $2) returning id', [@name, @coolness])
@id = result[0]['id']
else
CONN.exec_params('UPDATE instructors SET name = $1, coolness = $2 WHERE id = $3', [@name, @coolness, @id])
end
end
def destroy
CONN.exec_params('DELETE FROM instructors WHERE id = $1', [@id])
end
## DANGER Below is wet wet code. It's up to you to DRY it out and make it more succinct.
def self.find(id)
result = nil
CONN.exec_params('SELECT id, name, coolness FROM instructors WHERE id = $1 LIMIT 1', [id]) do |rows|
rows.each do |row|
result = Instructor.new(
row['name'],
row['coolness'],
row['id']
)
end
end
result
end
def self.all
results = []
CONN.exec_params('SELECT id, name, coolness FROM instructors') do |rows|
rows.each do |row|
results << Instructor.new(
row['name'],
row['coolness'],
row['id']
)
end
end
results
end
def self.where_coolness_above(coolness)
results = []
CONN.exec_params('SELECT id, name, coolness FROM instructors WHERE coolness > $1', [coolness]) do |rows|
rows.each do |row|
results << Instructor.new(
row['name'],
row['coolness'],
row['id']
)
end
end
results
end
end
require './instructor'
# david = Instructor.new('David', 1)
# david.save
#
# don = Instructor.new('Don', 50)
# don.save
#
# khurram = Instructor.new('Khurram', 5432)
# khurram.save
#
# arvinder = Instructor.new('Arvinder', 6)
# arvinder.save
# puts Instructor.find(1).inspect
# puts Instructor.find(2).inspect
# puts Instructor.find(3).inspect
# puts Instructor.find(4).inspect
# puts Instructor.find(5).inspect
# Instructor.all.each do |instructor|
# puts "#{instructor.name} is a #{instructor.coolness}"
# end
# instructor1 = Instructor.find(1)
# instructor1.coolness = 62
# instructor1.save
# puts Instructor.find(1).inspect
# monica = Instructor.new('Monica', 1000)
# monica.save
# puts monica.inspect
# instructor3 = Instructor.find(3)
# instructor3.destroy
Instructor.all.each do |instructor|
instructor.coolness = 9001
instructor.save
end
instructor2 = Instructor.find(2)
instructor2.coolness = 8999
instructor2.save
Instructor.where_coolness_above(9000).each do |instructor|
puts "#{instructor.name}'s coolness is OVER 9000!!!!!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment