updating: Add a person to a location

master
Matt Huntington 8 years ago
parent bee42d5fc2
commit c912091a7f

@ -12,8 +12,6 @@ class PeopleController < ApplicationController
def create def create
if params["id"] if params["id"]
params["person"]["home_id"] = params["id"].to_i params["person"]["home_id"] = params["id"].to_i
else
params["person"]["home_id"] = "NULL"
end end
render json: Person.create(params["person"]) render json: Person.create(params["person"])
end end

@ -7,7 +7,7 @@ class Person
@name = opts["name"] @name = opts["name"]
@age = opts["age"].to_i @age = opts["age"].to_i
if opts["home_id"] if opts["home_id"]
@home_id = opts["home_id"] @home_id = opts["home_id"].to_i
end end
end end
@ -24,9 +24,9 @@ class Person
def self.create(opts={}) def self.create(opts={})
results = DB.exec( results = DB.exec(
<<-SQL <<-SQL
INSERT INTO people (name, age) INSERT INTO people (name, age, home_id)
VALUES ( '#{opts["name"]}', #{opts["age"]} ) VALUES ( '#{opts["name"]}', #{opts["age"]}, #{opts["home_id"] ? opts["home_id"] : "NULL"})
RETURNING id, name, age; RETURNING id, name, age, home_id;
SQL SQL
) )
return Person.new(results.first) return Person.new(results.first)

Loading…
Cancel
Save