Rails accepted_nested_attributes_for错误,请帮我发现它

jfa*_*als 6 activerecord ruby-on-rails

我一直在尝试遵循Active Record Nested Attributes Guide,但没有取得多大成功.

我有以下型号:

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

class Telephone < ActiveRecord::Base
  belongs_to :contact
end
Run Code Online (Sandbox Code Playgroud)

在尝试创建联系人时:

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

我收到以下错误: ActiveRecord::AssociationTypeMismatch: Telephone(#80827590) expected, got Hash(#72886250)

你能帮我看看错误吗?我应该包含contact_controller.rb哪些代码?

jfa*_*als 10

我使用以下代码:

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

我把错误的参数传递给Contact.create控制器......