Ruby on rails 3.2 Rails 3为AssociationTypeMismatch失败的多态模型创建嵌套属性

Ruby on rails 3.2 Rails 3为AssociationTypeMismatch失败的多态模型创建嵌套属性,ruby-on-rails-3.2,rails-activerecord,polymorphic-associations,Ruby On Rails 3.2,Rails Activerecord,Polymorphic Associations,我有一个与地址模型具有多态关联的用户模型 class User < ActiveRecord::Base attr_accessible :addresses, :billing_addresses, :shipping_addresses, :shipping_same_as_billing has_many :addresses, dependent: :destroy has_many :billing_addresses, class_name: Address,

我有一个与地址模型具有多态关联的用户模型

class User < ActiveRecord::Base
  attr_accessible :addresses, :billing_addresses, :shipping_addresses, :shipping_same_as_billing

  has_many :addresses, dependent: :destroy

  has_many :billing_addresses, class_name: Address,  conditions: ['type = ?', :billing]
  has_many :shipping_addresses, class_name: Address,  conditions: ['type = ?', :shipping]
  accepts_nested_attributes_for :addresses
#...
end

class Address < ActiveRecord::Base
  belongs_to :user, polymorphic: true
  validates_presence_of :line1, :city, :state, :zip
  attr_accessible :line1, :line2, :city, :state, :zip, :type
#...
end
当我发布表单时,我收到以下错误:

ActiveRecord::AssociationTypeMismatch(地址(#70101930814600) 应为,已获取数组(70101927961780)):

我读了Stack上的另一篇文章,其中提到尝试改变

config.cache_classes = true
这没有做任何改变,我得到了相同的错误。我真的不想手动建立关联。似乎ActiveRecord可以处理这个问题,我只是错过了一些东西

--编辑如下以供评论

Parameters: {"utf8"=>"✓", "authenticity_token"=>"HigNtHH08ER1S6p3xMUPlreWMuHngjo5S8z74jeqsO0=", "creditcard"=>{"expiration(3i)"=>"1"}, "user"=>{"billing_addresses"=>{"type"=>"", "line1"=>"1653 Main", "line2"=>"", "city"=>"Redwood City", "state"=>"California", "zip"=>"94061"}, "email"=>"tom@tom.com", "password"=>"[FILTERED]", "shipping_addresses"=>{"shipping_same_as_billing"=>"0", "type"=>"", "line1"=>"1653 Main", "line2"=>"", "city"=>"Redwood City", "state"=>"California", "zip"=>"94061"}}, "stripeToken"=>"[FILTERED]"}
这些是我要派去的人。我现在正在控制器中调试:

Rails.logger.debug params['user']['billing_addresses']
Rails.logger.debug params['user']['shipping_addresses']
u = User.new(params["user"])
Rails.logger.debug u

{"type"=>"", "line1"=>"1653 Main", "line2"=>"", "city"=>"Redwood City", "state"=>"California", "zip"=>"94061"}
{"shipping_same_as_billing"=>"0", "type"=>"", "line1"=>"1653 Main", "line2"=>"", "city"=>"Redwood City", "state"=>"California", "zip"=>"94061"}

我看不出这个阵列是从哪里来的。除非ActiveRecord将
发货地址
账单地址
作为一个地址数组处理…

我相信您的模型用户需要一个
接受
的嵌套属性,所以请尝试将以下内容添加到
用户.rb

  accepts_nested_attributes_for :addresses
  accepts_nested_attributes_for :billing_addresses
  accepts_nested_attributes_for :shipping_addresses
除此之外,您还将遇到以下错误:

ActiveRecord::AssociationTypeMismatch(应为地址(#70101930814600),获得数组(#70101927961780))

当指定的对象类型不正确时,通常会引发此错误。所以把这个错误分解一下。似乎您的对象
地址
的类型不正确,但得到了一个数组。在某个地方,当你不应该的时候,你正在给一个数组喂食

另一个关键点是:多态关联的工作方式是在外键之外使用类型列来指定关联的记录。您需要一个可以将其指定给的列。因此,您可以举一个例子,如下所示:

class User < ActiveRecord::Base
      attr_accessible :addresses, :billing_addresses, :shipping_addresses,   
     :shipping_same_as_billing

      #Before       
      #has_many :addresses, dependent: :destroy

      #After
      has_many :addresses, :as => user, dependent => :destroy

   end 
class用户user,dependent=>:destroy
结束

进一步阅读:

看看我更新的答案,看看我提供的链接谢谢David。我最终将整个功能重构为更简单的功能。
class User < ActiveRecord::Base
      attr_accessible :addresses, :billing_addresses, :shipping_addresses,   
     :shipping_same_as_billing

      #Before       
      #has_many :addresses, dependent: :destroy

      #After
      has_many :addresses, :as => user, dependent => :destroy

   end