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,34 +22,57 @@ 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 = []
if result["home_id"] last_person_id = nil
home = Location.new( results.each do |result|
if result["id"] != last_person_id
last_person_id = result["id"]
if result["home_id"]
home = Location.new(
{
"id" => result["home_id"],
"street" => result["street"],
"city" => result["city"],
"state" => result["state"],
}
)
else
home = nil
end
new_person = Person.new(
{ {
"id" => result["home_id"], "id" => result["id"],
"street" => result["street"], "name" => result["name"],
"city" => result["city"], "age" => result["age"],
"state" => result["state"], "home" => home,
"employers" => []
} }
) )
else people.push(new_person)
home = nil 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
Person.new(
{
"id" => result["id"],
"name" => result["name"],
"age" => result["age"],
"home" => home,
}
)
end end
return people
end end
def self.find(id) def self.find(id)

Loading…
Cancel
Save