Retrieve the nextval from a sequence using activerecord in Ruby on Rails 3.2.14 / Ruby 2.0.0 / PostgreSQL 9.2.4
Asked Answered
F

4

12

This should be SO simple. I want to retrieve the nextval of a sequence... it's not a default value... it's not a primary key... it's not a foreign key. In rare cases, I need a sequence number for a user supplied value.

I've tried the following:

@nextid = ActiveRecord::Base.connection.execute("SELECT nextval('xscrpt_id_seq')")

and what I get back is:

#<PG::Result:0x007fe668a854e8 @connection=#<PG::Connection:0x00000003aeff30>>

And by using

@nextid[0]["nextval"]

I can get the correct value, but it doesn't seem like the right way to approach the problem. I've searched, I read "Pro Active Record", which said to use:

M_script.find_by_sql("SELECT nextval('xscript_id_seq')")

but, that didn't work.

Any hints on the "correct" (Rails way) to retrieve a nextval from a sequence in ROR, would be very appreciated!

Fractocumulus answered 30/7, 2013 at 18:22 Comment(0)
F
12

I believe:

ActiveRecord::Base.connection.execute( "SELECT nextval('xscrpt_id_seq')" )

is the correct solution. As I said in my original question, I checked the book "Pro Active Record" and it is their recommended solution. I'm still not exactly sure how ActiveRecord is making the connection, or if there's any maintenance I need to do (such as closing it).

ActiveRecord used to have a next_sequence_number method, but that has been deprecated.

Fractocumulus answered 31/7, 2013 at 15:4 Comment(0)
H
10

You probably want to use select_value and sequence_name:

Xscrpt.connection.select_value("select nextval('#{Xscrpt.sequence_name}')").to_i
Harkness answered 21/1, 2016 at 20:30 Comment(2)
select_value function is super helpful and no need to fetch like [0]['nextval']. Thanks @HarknessPlethoric
You don't need the #to_i at the end, #select_value casts the value to Integer for you.Graciagracie
R
1

This works too:

 @seq = Smodelname.find_by_sql(" select seq_name.nextval as id from dual ")[0][:id]
Route answered 15/5, 2018 at 8:37 Comment(0)
J
1

At least with Rails 5.1.7, be careful:

ActiveRecord::Base.connection.select_value

increments the sequence within a transaction only for the first call. When you execute it multiple times, you'll get always the same value and the sequence is not incremented.

ActiveRecord::Base.connection.execute

does not cache and works as expected

Jacquesjacquet answered 5/6, 2021 at 6:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.