|
|
|
@ -7,19 +7,84 @@ class Person
|
|
|
|
@id = opts["id"].to_i
|
|
|
|
@id = opts["id"].to_i
|
|
|
|
@name = opts["name"]
|
|
|
|
@name = opts["name"]
|
|
|
|
@age = opts["age"].to_i
|
|
|
|
@age = opts["age"].to_i
|
|
|
|
if opts["home_id"]
|
|
|
|
if opts["home"]
|
|
|
|
@home = Location.find(opts["home_id"].to_i)
|
|
|
|
@home = opts["home"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def self.all
|
|
|
|
def self.all
|
|
|
|
results = DB.exec("SELECT * FROM people;")
|
|
|
|
results = DB.exec(
|
|
|
|
return results.map { |result| Person.new(result) }
|
|
|
|
<<-SQL
|
|
|
|
|
|
|
|
SELECT
|
|
|
|
|
|
|
|
people.*,
|
|
|
|
|
|
|
|
locations.street,
|
|
|
|
|
|
|
|
locations.city,
|
|
|
|
|
|
|
|
locations.state
|
|
|
|
|
|
|
|
FROM people
|
|
|
|
|
|
|
|
LEFT JOIN locations
|
|
|
|
|
|
|
|
ON people.home_id = locations.id
|
|
|
|
|
|
|
|
SQL
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
return results.map do |result|
|
|
|
|
|
|
|
|
if result["home_id"]
|
|
|
|
|
|
|
|
home = Location.new(
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
"id" => result["home_id"],
|
|
|
|
|
|
|
|
"street" => result["street"],
|
|
|
|
|
|
|
|
"city" => result["city"],
|
|
|
|
|
|
|
|
"state" => result["state"],
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
home = nil
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
Person.new(
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
"id" => result["id"],
|
|
|
|
|
|
|
|
"name" => result["name"],
|
|
|
|
|
|
|
|
"age" => result["age"],
|
|
|
|
|
|
|
|
"home" => home,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def self.find(id)
|
|
|
|
def self.find(id)
|
|
|
|
results = DB.exec("SELECT * FROM people WHERE id=#{id};")
|
|
|
|
results = DB.exec(
|
|
|
|
return Person.new(results.first)
|
|
|
|
<<-SQL
|
|
|
|
|
|
|
|
SELECT
|
|
|
|
|
|
|
|
people.*,
|
|
|
|
|
|
|
|
locations.street,
|
|
|
|
|
|
|
|
locations.city,
|
|
|
|
|
|
|
|
locations.state
|
|
|
|
|
|
|
|
FROM people
|
|
|
|
|
|
|
|
LEFT JOIN locations
|
|
|
|
|
|
|
|
ON people.home_id = locations.id
|
|
|
|
|
|
|
|
WHERE people.id=#{id};
|
|
|
|
|
|
|
|
SQL
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
result = results.first
|
|
|
|
|
|
|
|
if result["home_id"]
|
|
|
|
|
|
|
|
home = Location.new(
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
"id" => result["home_id"],
|
|
|
|
|
|
|
|
"street" => result["street"],
|
|
|
|
|
|
|
|
"city" => result["city"],
|
|
|
|
|
|
|
|
"state" => result["state"],
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
home = nil
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
person = Person.new(
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
"id" => result["id"],
|
|
|
|
|
|
|
|
"name" => result["name"],
|
|
|
|
|
|
|
|
"age" => result["age"],
|
|
|
|
|
|
|
|
"home" => home,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
return person
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def self.create(opts={})
|
|
|
|
def self.create(opts={})
|
|
|
|
|