Expand a foreign key to an object

master
Matt Huntington 8 years ago
parent ad206cb128
commit d9c43392d6

@ -1,5 +1,5 @@
class Location
attr_reader :id, :street, :city, :state
attr_reader :id, :street, :city, :state, :inhabitants
# connect to postgres
DB = PG.connect(host: "localhost", port: 5432, dbname: 'contacts')
@ -8,6 +8,7 @@ class Location
@street = opts["street"]
@city = opts["city"]
@state = opts["state"]
@inhabitants = Person.findByHomeId(@id)
end
def self.all

@ -1,5 +1,5 @@
class Person
attr_reader :id, :name, :age, :home_id
attr_reader :id, :name, :age, :home
# connect to postgres
DB = PG.connect(host: "localhost", port: 5432, dbname: 'contacts')
@ -8,7 +8,7 @@ class Person
@name = opts["name"]
@age = opts["age"].to_i
if opts["home_id"]
@home_id = opts["home_id"].to_i
@home = Location.find(opts["home_id"].to_i)
end
end
@ -61,4 +61,9 @@ class Person
)
return Person.new(results.first)
end
def self.findByHomeId(home_id)
results = DB.exec("SELECT id, name, age FROM people WHERE home_id = #{home_id}")
return results.map { |result| Person.new(result) }
end
end

Loading…
Cancel
Save