Nested forms and automatic creation of parent, children

Posted by Karen on Stack Overflow See other posts from Stack Overflow or by Karen
Published on 2010-02-22T06:24:10Z Indexed on 2010/04/15 23:03 UTC
Read the original article Hit count: 503

Filed under:
|
|

Guys,

I was wondering if it was possible to create new parent, children in a has many relationship, using rails nested forms.

Rails documentation clearly says that this works in a one to one relationship. Not sure if its the same in has many relationship.

For example:

If

params = { 
  :employee => { 
    :name => "Tester", 
    :account_attributes => {:login => 'tester'}
  }
} 

works as one to one relationship. So Employee.new(params) works fine. New employee, account are created.

Supposing I had

params = { 
  :employee => { 
    :name => "Tester", 
    :account_attributes => {
      "0" => {:login => 'tester'}, 
      "1" => {:login => 'tester2'}
    }
  }
} 

Employee.new(params) doesnt work. It fails on child validations saying parent cant be blank.

Any help is appreciated. Thanks

Karen

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about nested