has_one through和accepts_nested_attributes_for

时间:2014-10-22 09:43:32

标签: ruby-on-rails ruby has-one-through

我正在尝试创建一个表单来编辑由has_one通过关系链接的2个对象:

bank_acount.rb

class BankAccount < ActiveRecord::Base
  belongs_to :user, inverse_of: :bank_account
  has_one :personal_information, through: :user
  accepts_nested_attributes_for :personal_information
  accepts_nested_attributes_for :user
  #...
end

personal_information.rb

class PersonalInformation < ActiveRecord::Base
  belongs_to :user, inverse_of: :personal_information
  #...
end

user.rb

class User < ActiveRecord::Base
  has_one :personal_information, inverse_of: :user
  has_one :bank_account, inverse_of: :user
  accepts_nested_attributes_for :personal_information
  #...
end

当我尝试提交表单时(这是一个模拟它的测试)

bank_account.update({:iban => 'XXX', :bic => 'XXX', personal_information_attributes: {
    first_name: 'first name',
    last_name: 'last name',
    address: 'address',
    zip: 'zip',
    city: 'city'
}})

我收到此错误:

ArgumentError: Cannot build association `personal_information'. Are you trying to build a polymorphic one-to-one association?

我对多态的东西一无所知,我不知道这个错误来自哪里。

有什么想法吗?

0 个答案:

没有答案