Show each person's jobs

master
Matt Huntington 8 years ago
parent 8d410f6d37
commit b6d0c3d8ef

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

Loading…
Cancel
Save