Set up model class initializer to take params

master
Matt Huntington 8 years ago
parent 0e586ac470
commit 6d8af5dd03

@ -2,15 +2,14 @@ class Person
# connect to postgres # connect to postgres
DB = PG.connect(host: "localhost", port: 5432, dbname: 'contacts') 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 def self.all
results = DB.exec("SELECT * FROM people;") results = DB.exec("SELECT * FROM people;")
results.each do |result| return results.map { |result| Person.new(result) }
puts result
end
[
{ name: 'Joey', age:12 },
{ name: 'Sarah', age:52 },
{ name: 'Cthulhu', age: 8000 }
]
end end
end end

Loading…
Cancel
Save