I'm using the 'pg' gem to work with postgresql from ruby. When inserting a new record, how can I get its serial id?
DB = PG.connect dbname: 'mydb', user: 'myuser' res = DB.exec "insert into ..."
I've found out you can add returning id to the query string. I.e.
returning id
DB = PG.connect dbname: 'mydb', user: 'myuser' res = DB.exec "insert into ..." id = res.first['id']
Source