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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With