0

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 ..."
dimid
  • 7,285
  • 1
  • 46
  • 85

1 Answers1

1

I've found out you can add returning id to the query string. I.e.

DB = PG.connect dbname: 'mydb', user: 'myuser'
res = DB.exec "insert into ..."
id = res.first['id']

Source

Community
  • 1
  • 1
dimid
  • 7,285
  • 1
  • 46
  • 85