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.

Revisions

  1. David VanDusen revised this gist Mar 17, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions create_table.sql
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    CREATE TABLE instructors (
    id serial PRIMARY KEY NOT NULL,
    name VARCHAR(100),
    coolness INT DEFAULT 9001
    id SERIAL PRIMARY KEY NOT NULL,
    name VARCHAR(100),
    coolness INT DEFAULT 5
    );
  2. David VanDusen revised this gist Mar 17, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion create_table.sql
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    CREATE TABLE instructors (
    id serial PRIMARY KEY NOT NULL,
    name VARCHAR(100),
    coolness INT DEFAULT 5
    coolness INT DEFAULT 9001
    );
  3. David VanDusen created this gist Mar 17, 2015.
    1 change: 1 addition & 0 deletions Gemfile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    gem 'pg'
    5 changes: 5 additions & 0 deletions create_table.sql
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    CREATE TABLE instructors (
    id serial PRIMARY KEY NOT NULL,
    name VARCHAR(100),
    coolness INT DEFAULT 5
    );
    88 changes: 88 additions & 0 deletions instructor.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,88 @@
    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
    50 changes: 50 additions & 0 deletions main.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    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