From d5558fc2ee4f043acc583aa4b7272429a73ac51e Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Thu, 15 Mar 2018 20:06:19 -0400 Subject: [PATCH] fruit index gets with postgres --- app/models/fruit.rb | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/app/models/fruit.rb b/app/models/fruit.rb index e56dd51..389c6ad 100644 --- a/app/models/fruit.rb +++ b/app/models/fruit.rb @@ -1,9 +1,15 @@ class Fruit + DB = PG.connect(host: "localhost", port: 5432, dbname: 'simplerails') + + def initialize(opts = {}) + @id = opts["id"].to_i + @name = opts["name"] + @color = opts["color"] + @readyToEat = (opts["readytoeat"]=='t')?true:false + end + def self.all - [ - { name: 'apple', color: 'red', readyToEat: true }, - { name: 'banana', color: 'yellow', readyToEat: false }, - { name: 'orange', color: 'orange', readyToEat: false } - ] + results = DB.exec("SELECT * FROM fruits;") + return results.map { |fruit_opts| Fruit.new(fruit_opts)} end end