HeisenbergsUncertaintyThizz
2010-Sep-17 23:51 UTC
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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Schuerig
2010-Sep-18 11:17 UTC
Re: Postgresql RETURNING statement -> returning and ID after writing to a DB
On Saturday 18 September 2010, HeisenbergsUncertaintyThizz wrote:> 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 dir>/activerecord-3.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb Michael -- Michael Schuerig mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org http://www.schuerig.de/michael/ -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
HeisenbergsUncertaintyThizz
2010-Sep-18 11:18 UTC
Re: Postgresql RETURNING statement -> returning and ID after writing to a DB
Thank you very much. On Sep 18, 7:17 am, Michael Schuerig <mich...-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org> wrote:> On Saturday 18 September 2010, HeisenbergsUncertaintyThizz wrote: > > > 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 > dir>/activerecord-3.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb > > Michael > > -- > Michael Schuerig > mailto:mich...-q5aiKMLteq5BV9CJdY2HSA@public.gmane.org://www.schuerig.de/michael/-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.