2010-11-07 23 views
6

He estado tratando de seguir el Active Record Nested Attributes Guide, sin mucho éxito.Rails accepts_nested_attributes_for Error, ayúdeme a detectarlo

tengo los siguientes modelos:

class Contact < ActiveRecord::Base 
    has_many :telephones 
    accepts_nested_attributes_for :telephones 
end 

class Telephone < ActiveRecord::Base 
    belongs_to :contact 
end 

Al intentar crear un contacto:

contact = { 
    :name => "John", 
    :telephones => [ 
    {:telephone => '787445741'}, 
    {:telephone => '478589658'} 
    ] 
} 
Contact.create(contact) 

me sale el siguiente error: ActiveRecord::AssociationTypeMismatch: Telephone(#80827590) expected, got Hash(#72886250)

Podría usted por favor me ayuden a Spot the ¿error? ¿Hay algún código que deba incluir en el contact_controller.rb?

Respuesta

10

lo tengo trabajando con el siguiente código:

params = { :contact => { 
    :name => 'Joe', 
    :permanentcomment => "No Comment", 
    :telephones_attributes => [ 
     {:telephone => '787445741'}, 
     {:telephone => '478589658'} 
    ] 
    }} 
    Contact.create(params[:contact]) 

yo estaba pasando los argumentos equivocados al controlador Contact.create ...

Cuestiones relacionadas