|
|
|
@ -1,5 +1,5 @@
|
|
|
|
class Company
|
|
|
|
class Company
|
|
|
|
attr_reader :id, :name, :industry
|
|
|
|
attr_reader :id, :name, :industry, :employees
|
|
|
|
# connect to postgres
|
|
|
|
# connect to postgres
|
|
|
|
DB = PG.connect(host: "localhost", port: 5432, dbname: 'contacts')
|
|
|
|
DB = PG.connect(host: "localhost", port: 5432, dbname: 'contacts')
|
|
|
|
|
|
|
|
|
|
|
|
@ -7,6 +7,9 @@ class Company
|
|
|
|
@id = opts["id"].to_i
|
|
|
|
@id = opts["id"].to_i
|
|
|
|
@name = opts["name"]
|
|
|
|
@name = opts["name"]
|
|
|
|
@age = opts["industry"]
|
|
|
|
@age = opts["industry"]
|
|
|
|
|
|
|
|
if opts["employees"]
|
|
|
|
|
|
|
|
@employees = opts["employees"]
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def self.all
|
|
|
|
def self.all
|
|
|
|
@ -15,8 +18,36 @@ class Company
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def self.find(id)
|
|
|
|
def self.find(id)
|
|
|
|
results = DB.exec("SELECT * FROM companies WHERE id=#{id};")
|
|
|
|
results = DB.exec(<<-SQL
|
|
|
|
return Company.new(results.first)
|
|
|
|
SELECT
|
|
|
|
|
|
|
|
companies.*,
|
|
|
|
|
|
|
|
people.id AS person_id,
|
|
|
|
|
|
|
|
people.name AS person_name,
|
|
|
|
|
|
|
|
people.age
|
|
|
|
|
|
|
|
FROM companies
|
|
|
|
|
|
|
|
LEFT JOIN jobs
|
|
|
|
|
|
|
|
ON companies.id = jobs.company_id
|
|
|
|
|
|
|
|
LEFT JOIN people
|
|
|
|
|
|
|
|
ON jobs.person_id = people.id
|
|
|
|
|
|
|
|
WHERE companies.id=#{id};
|
|
|
|
|
|
|
|
SQL
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
employees = [];
|
|
|
|
|
|
|
|
results.each do |result|
|
|
|
|
|
|
|
|
if result["person_id"]
|
|
|
|
|
|
|
|
employees.push(Person.new({
|
|
|
|
|
|
|
|
"id" => result["person_id"],
|
|
|
|
|
|
|
|
"name" => result["person_name"],
|
|
|
|
|
|
|
|
"age" => result["age"]
|
|
|
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
return Company.new({
|
|
|
|
|
|
|
|
"id" => results.first["id"],
|
|
|
|
|
|
|
|
"name" => results.first["name"],
|
|
|
|
|
|
|
|
"industry" => results.first["industry"],
|
|
|
|
|
|
|
|
"employees" => employees
|
|
|
|
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def self.create(opts={})
|
|
|
|
def self.create(opts={})
|
|
|
|
|