Postgresql RETURNING statement -> returning and ID after writing to a DB

Currently, I have an Articles page which creates articles in a DB.

What I would like to do is to return the ID of the submitted article.

I know how to do it with raw sql,

"INSERT INTO test (name) VALUES ('My Name 1') RETURNING id; "

but I was wondering if there is a way to do it, "The Rails Way".

Still very new to RoR.

Currently, I have an Articles page which creates articles in a DB.

What I would like to do is to return the ID of the submitted article.

I know how to do it with raw sql,

"INSERT INTO test (name) VALUES ('My Name 1') RETURNING id; "

but I was wondering if there is a way to do it, "The Rails Way".

You just do

  @article = Article.create!(...)

and @article will have the correct ID. Internally, the postgresql adapter uses RETURNING if it is supported by the PostgreSQL version (>= 8.0.2 or something).

If you're interested in the details, read the code in <gem

/activerecord-3.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb

Michael

Thank you very much.