ActiveRecord and working with sequences

Howdy,

(sorry if this ends up as a duplicate post, my subscription was messed up)

I'm very new with ruby and ActiveRecord.

I've got a table in postgres that looks something like

create table test
    test_id not null default nextval('test_id_seq')
    test_parent int not null
    test_name varchar(20) not null

For the parent record, test_parent = test_id.
I can't change the schema, so i need to work with the table structure I have.

What's the best way to handle that situation in ActiveRecord?

It seems like i should do something like:
SEQ = <code to select nextval('test_id_seq')>
Test.new(:test_id => SEQ, :test_parent => SEQ, :test_name => 'woohoo' );

I'm not sure how to active the <code to select nextval()> portion with ActiveRecord.

I tried:
seq = ActiveRecord::Base.connection.execute("SELECTnextval('test_id_seq')")

And that seems to be doable, but the PG:Result class is a little weird, so I was hoping there was a better way.

Thanks.

Dave

Howdy,

(sorry if this ends up as a duplicate post, my subscription was messed up)

I'm very new with ruby and ActiveRecord.

The first thing you should know is that there is a Ruby on Rails
specific group where rails-specific questions should go:

https://groups.google.com/forum/?fromgroups#!forum/rubyonrails-talk

I've got a table in postgres that looks something like

create table test
test_id not null default nextval('test_id_seq')
test_parent int not null
test_name varchar(20) not null

For the parent record, test_parent = test_id.
I can't change the schema, so i need to work with the table structure I
have.

What's the best way to handle that situation in ActiveRecord?

It seems like i should do something like:
SEQ = <code to select nextval('test_id_seq')>
Test.new(:test_id => SEQ, :test_parent => SEQ, :test_name => 'woohoo' );

I'm not sure how to active the <code to select nextval()> portion with
ActiveRecord.

I tried:
seq = ActiveRecord::Base.connection.execute("SELECTnextval('test_id_seq')")

And that seems to be doable, but the PG:Result class is a little weird, so I
was hoping there was a better way.

Thanks.

Dave

As for using PostgreSQL sequences, check out:

http://api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html#method-i-sequence_name-3D

···

On Thu, Mar 22, 2012 at 12:22 PM, David Kerr <dmk@mr-paradox.net> wrote:

--
Kendall Gifford
zettabyte@gmail.com

Howdy,

(sorry if this ends up as a duplicate post, my subscription was messed up)

I'm very new with ruby and ActiveRecord.

The first thing you should know is that there is a Ruby on Rails
specific group where rails-specific questions should go:

Redirecting to Google Groups

Great, thanks!

So ActiveRecord is a full on component of Rails? I understand that it's most frequently used with rails, but I thought that rails was just the MVC framework for web development.

As for using PostgreSQL sequences, check out:

ActiveRecord::ModelSchema::ClassMethods

thanks, I had seen that one, it tells me how to set the sequence name, but not how to set field2 = field1 = sequence.next elegantly.

Anyway, thanks for the tips. I'll check out the Rails group!

Dave

···

On 03/22/2012 03:54 PM, Kendall Gifford wrote:

On Thu, Mar 22, 2012 at 12:22 PM, David Kerr<dmk@mr-paradox.net> wrote: