Skip to content

Instantly share code, notes, and snippets.

@nmajor
Created February 2, 2018 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nmajor/db982148348fdbfdb52a9eea24dc691c to your computer and use it in GitHub Desktop.
Save nmajor/db982148348fdbfdb52a9eea24dc691c to your computer and use it in GitHub Desktop.
require 'sqlite3'
DB = SQLite3::Database.new('test.db')
class Doctor
attr_reader :id
def initialize(attributes = {})
@id = attributes[:id]
@name = attributes[:name]
@age = attributes[:age]
@specialty = attributes[:specialty]
end
def self.all
rows = DB.execute('SELECT * FROM doctors');
end
def is_new?
@id.nil?
end
def new?
is_new?
end
def has_been_saved?
!is_new?
end
def save
query = "INSERT INTO doctors (name, age, specialty) VALUES ('#{@name}', #{@age}, '#{@specialty}')"
DB.execute(query);
@id = DB.last_insert_row_id
end
end
quinn = Doctor.new(name: 'Harley Quinn', age: 35)
p quinn
p quinn.is_new?
quinn.save
p quinn
p quinn.is_new?
quinn.age = quinn.age + 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment