From 0e586ac470fb2b9eda5ac3b4ee759f35f59166c6 Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Mon, 19 Mar 2018 09:11:30 -0400 Subject: [PATCH] Make a model connect to Postgres --- app/models/person.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/app/models/person.rb b/app/models/person.rb index 38bc5e2..a167c17 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -1,6 +1,13 @@ class Person - def self.all # a static function that's called on the class itself, not an instance - [ ## ruby functions return the last line of code, so no need for an explicit 'return' statement + # connect to postgres + DB = PG.connect(host: "localhost", port: 5432, dbname: 'contacts') + + def self.all + results = DB.exec("SELECT * FROM people;") + results.each do |result| + puts result + end + [ { name: 'Joey', age:12 }, { name: 'Sarah', age:52 }, { name: 'Cthulhu', age: 8000 }