Why is my new ID always "1"

Posted by normalocity on Stack Overflow See other posts from Stack Overflow or by normalocity
Published on 2010-04-27T01:57:36Z Indexed on 2010/04/27 2:03 UTC
Read the original article Hit count: 412

Filed under:
|

I have a parent-child relationship between two objects.

Parent :has_many :children
Child  :belongs_to :parent

When creating a new parent, in the same controller, I'm creating the child.

@mom = Parent.new
@child = Child.new
@mom.children << @child

That all seems to go okay, but this parent has one more attribute - this parent has a favorite child

@mom.favorite_child = @child

Seems like this should work, except let's say that this is the 61st child in the database, so it gets an ID of 61 (and I know this is happening, because when I check the database, the child record has an ID of 61). For some reason, when I assign the @child to the parent's "favorite_child" attribute, "favorite_child" gets set to "1" - when I need it to be set to "61".

Clues?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about activerecord