person.all for many to many

master
Matt Huntington 8 years ago
parent b6d0c3d8ef
commit ca48eaa121

@ -27,10 +27,9 @@ class Location
SQL SQL
) )
locations = [] locations = []
current_location_id = nil last_location_id = nil
results.each do |result| results.each do |result|
if result["id"] != current_location_id if result["id"] != last_location_id
current_location_id = result["id"]
locations.push( locations.push(
Location.new({ Location.new({
"id" => result["id"], "id" => result["id"],
@ -40,6 +39,7 @@ class Location
"inhabitants" => [] "inhabitants" => []
}) })
) )
last_location_id = result["id"]
end end
if result["person_id"] if result["person_id"]
new_person = Person.new({ new_person = Person.new({

@ -22,13 +22,24 @@ 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
SQL SQL
) )
return results.map do |result| people = []
last_person_id = nil
results.each do |result|
if result["id"] != last_person_id
last_person_id = result["id"]
if result["home_id"] if result["home_id"]
home = Location.new( home = Location.new(
{ {
@ -41,15 +52,27 @@ class Person
else else
home = nil home = nil
end end
Person.new( 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" => []
} }
) )
people.push(new_person)
end
if result["company_id"]
employer = Company.new({
"id" => result["company_id"],
"name" => result["company"],
"industry" => result["industry"]
})
people.last.employers.push(employer)
end
end end
return people
end end
def self.find(id) def self.find(id)

Loading…
Cancel
Save