Show each person's jobs

master
Matt Huntington 8 years ago
parent 8d410f6d37
commit b6d0c3d8ef

@ -1,5 +1,5 @@
class Person class Person
attr_reader :id, :name, :age, :home attr_reader :id, :name, :age, :home, :employers
# connect to postgres # connect to postgres
DB = PG.connect(host: "localhost", port: 5432, dbname: 'contacts') DB = PG.connect(host: "localhost", port: 5432, dbname: 'contacts')
@ -10,6 +10,9 @@ class Person
if opts["home"] if opts["home"]
@home = opts["home"] @home = opts["home"]
end end
if opts["employers"]
@employers = opts["employers"]
end
end end
def self.all def self.all
@ -56,10 +59,17 @@ class Person
people.*, people.*,
locations.street, locations.street,
locations.city, locations.city,
locations.state locations.state,
companies.id AS company_id,
companies.name AS company,
companies.industry
FROM people FROM people
LEFT JOIN locations LEFT JOIN locations
ON people.home_id = locations.id ON people.home_id = locations.id
LEFT JOIN jobs
ON people.id = jobs.person_id
LEFT JOIN companies
ON jobs.company_id = companies.id
WHERE people.id=#{id}; WHERE people.id=#{id};
SQL SQL
) )
@ -76,12 +86,20 @@ class Person
else else
home = nil home = nil
end end
employers = results.map do |result|
Company.new({
"id" => result["company_id"],
"name" => result["company"],
"industry" => result["industry"],
})
end
person = Person.new( person = Person.new(
{ {
"id" => result["id"], "id" => result["id"],
"name" => result["name"], "name" => result["name"],
"age" => result["age"], "age" => result["age"],
"home" => home, "home" => home,
"employers" => employers
} }
) )
return person return person

Loading…
Cancel
Save