Forum: Ruby on Rails Nuube Sequence problem

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cd8c9864d88bcafc164d8fdb820cc451?d=identicon&s=25 Chris (Guest)
on 2006-03-21 17:36
In my database the id for a field is 810.  RoR thinks that it is 809.
My log file (below) shows that just before the insert it grabs the next
sequence value. I dont think it should do that. This causes problems
further down the line when trying to access the Person object.

Development Log file :
"SQL (0.016000)   select people_seq.nextval id from dual

Person Create (0.015000)   INSERT INTO people (title, weight,
mothers_maiden_name, shoe_size, middlename, person_type, username,
firstname, height, person_id, createdby, expired, name_string, surname)
VALUES('Mr', '100', '', 10, 'T', null, null, 'T', '', :id, 'Chris
Richards', 0, null, 'T')"

My Code  (standard generated code):

  def create_person
    @person = Person.new(params[:person])
    if @person.save
      flash[:notice] = 'Person was successfully created.'
      .....
    else
      ......
    end
  end

Its wierd, ive used rails for weeks with no problems.

Any ideas?
Cd8c9864d88bcafc164d8fdb820cc451?d=identicon&s=25 Chris (Guest)
on 2006-03-21 17:43
This is what i have done to solve it, but it is totally wrong/stupid:
Notice "@person.id+=1"

if @person.save
    @person.id+=1 #!!!!!
    flash[:notice] = 'Person was successfully created.'
    ...
else
    ...
end

This makes me feel dirty!! anyone any ideas wtf is going on?
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-03-21 17:51
(Received via mailing list)
Chris wrote:
> This is what i have done to solve it, but it is totally wrong/stupid:
> Notice "@person.id+=1"
<snip>
> This makes me feel dirty!! anyone any ideas wtf is going on?
Which DB are you using, and why do you think the database has a
different idea about the value of an ID to Rails?
Cd8c9864d88bcafc164d8fdb820cc451?d=identicon&s=25 Chris (Guest)
on 2006-03-21 17:56
I'm using Oracle.

I get this error when i try to use the person object:

"Couldn't find Person with ID=825.0"

It actually has an id of 826 in the DB.  Strange.

Thanks,
Chris
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-03-21 18:01
(Received via mailing list)
Chris wrote:
> I'm using Oracle.
>
> I get this error when i try to use the person object:
>
> "Couldn't find Person with ID=825.0"
>
> It actually has an id of 826 in the DB.  Strange.
>
Not an Oracle user myself, so I can't help that much, but it might be
worth checking the column type of the id field.  Other than that, not
sure.
E626ea8ee039ce201b097fbec8e214d7?d=identicon&s=25 Eduardo Rebouças (Guest)
on 2006-03-21 18:06
(Received via mailing list)
Hi Chris,

I´ve done some tests with Oracle and it worked perfectly well. I did
only a small trick to avoid getting that decimal point on the ID: on
the view we use :id => doc.id.round.

[]´s

2006/3/21, Chris <evilgeenius@gmail.com>:
D1fb4423204d9440a1b7aacce50eb641?d=identicon&s=25 Corey Lawson (Guest)
on 2006-03-21 18:12
(Received via mailing list)
Do you have a trigger on the table that also fetches the ID from the
sequence when you're inserting records? If so, you might want to
modify the trigger so that if the new ID is passed in, to use it,
otherwise grab a value from the sequence, something like:

create or modify trigger mytable_bi
before insert on mytable
for each row
if :new.id is null then
  insert into mytable values (myseq.nextval, ...)
else
  insert into mytable values (:new.id, ...)
endif
end
Cd8c9864d88bcafc164d8fdb820cc451?d=identicon&s=25 Chris (Guest)
on 2006-03-21 18:23
Thanks Corey,

I'm using a legacy DB and a trigger existed that grabbed the next value
from the sequence. I never thought of that!!

Chris
This topic is locked and can not be replied to.