You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

21 lines
513 B

class Person
# connect to postgres
DB = PG.connect(host: "localhost", port: 5432, dbname: 'contacts')
def initialize(opts = {})
@id = opts["id"].to_i
@name = opts["name"]
@age = opts["age"]
end
def self.all
results = DB.exec("SELECT * FROM people;")
return results.map { |result| Person.new(result) }
end
def self.find(id)
results = DB.exec("SELECT * FROM people WHERE id=#{id};")
return Person.new(results.first)
end
end