ruby on rails - Why is my new ID always "1" -


i have parent-child relationship between 2 objects.

parent :has_many :children child  :belongs_to :parent 

when creating new parent, in same controller, i'm creating child.

@mom = parent.new @child = child.new @mom.children << @child 

that seems go okay, parent has 1 more attribute - parent has favorite child

@mom.favorite_child = @child 

seems should work, except let's 61st child in database, gets id of 61 (and know happening, because when check database, child record has id of 61). reason, when assign @child parent's "favorite_child" attribute, "favorite_child" gets set "1" - when need set "61".

clues?

seems parent needs like

class parent   has_many :children   has_one :favorite_child, :foreign_key=>'favorite_child_id', :class_name => 'child' 

otherwise, doesn't know it's foreign key relationship, , you're trying assign object integer.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -