How to get the id of the row inserted from PG::Result

database, heroku, insert, pg, postgresql

Solution

res  = conn.exec("INSERT INTO names (name) VALUES ('john') returning *")
res[0]
res[0]['id']

I used `returning *` just to show you can return everything not just the id. But obviously if you only need the id or the id and some other column use the explicit form just as you would in a select list

returning id, name

Problem

I've ran a simple insert command: `INSERT INTO names (name) VALUES ('john')` As a response I get a PG::Result object. I've been digging through those docs but I can't squeeze out of that object the info I need: what is the ID of the row I've just inserted?

Original source