diff --git a/app/models/company.rb b/app/models/company.rb index 0d2e38d..1ce02c6 100644 --- a/app/models/company.rb +++ b/app/models/company.rb @@ -19,8 +19,14 @@ class Company end def self.create(opts={}) - results = DB.exec("INSERT INTO companies (name, industry) VALUES ( '#{opts["name"]}', '#{opts["industry"]}' );") - return { created:true } + results = DB.exec( + <<-SQL + INSERT INTO companies (name, industry) + VALUES ( '#{opts["name"]}', '#{opts["industry"]}' ) + RETURNING id, name, industry; + SQL + ) + return Company.new(results.first) end def self.delete(id) @@ -29,7 +35,14 @@ class Company end def self.update(id, opts={}) - results = DB.exec("UPDATE companies SET name='#{opts["name"]}', industry='#{opts["industry"]}' WHERE id=#{id} ;") - return { updated: true } + results = DB.exec( + <<-SQL + UPDATE companies + SET name='#{opts["name"]}', industry='#{opts["industry"]}' + WHERE id=#{id} + RETURNING id, name, industry; + SQL + ) + return Company.new(results.first) end end diff --git a/app/models/location.rb b/app/models/location.rb index e962029..b3912c6 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -20,8 +20,14 @@ class Location end def self.create(opts={}) - results = DB.exec("INSERT INTO locations (street, city, state) VALUES ( '#{opts["street"]}', '#{opts["city"]}', '#{opts["state"]}' );") - return { created:true } + results = DB.exec( + <<-SQL + INSERT INTO locations (street, city, state) + VALUES ( '#{opts["street"]}', '#{opts["city"]}', '#{opts["state"]}' ) + RETURNING id, street, city, state; + SQL + ) + return Location.new(results.first) end def self.delete(id) @@ -30,7 +36,14 @@ class Location end def self.update(id, opts={}) - results = DB.exec("UPDATE locations SET street='#{opts["street"]}', city='#{opts["city"]}', state='#{opts["state"]}' WHERE id=#{id} ;") - return { updated: true } + results = DB.exec( + <<-SQL + UPDATE locations + SET street='#{opts["street"]}', city='#{opts["city"]}', state='#{opts["state"]}' + WHERE id=#{id} + RETURNING id, street, city, state; + SQL + ) + return Location.new(results.first) end end diff --git a/app/models/person.rb b/app/models/person.rb index f0b4c40..bc0fe54 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -22,8 +22,14 @@ class Person end def self.create(opts={}) - results = DB.exec("INSERT INTO people (name, age, home_id) VALUES ( '#{opts["name"]}', #{opts["age"]}, #{opts["home_id"]} );") - return { created:true } + results = DB.exec( + <<-SQL + INSERT INTO people (name, age) + VALUES ( '#{opts["name"]}', #{opts["age"]} ) + RETURNING id, name, age; + SQL + ) + return Person.new(results.first) end def self.delete(id) @@ -32,7 +38,14 @@ class Person end def self.update(id, opts={}) - results = DB.exec("UPDATE people SET name='#{opts["name"]}', age=#{opts["age"]} WHERE id=#{id} ;") - return { updated: true } + results = DB.exec( + <<-SQL + UPDATE people + SET name='#{opts["name"]}', age=#{opts["age"]} + WHERE id=#{id} + RETURNING id, name, age; + SQL + ) + return Person.new(results.first) end end