我的模型看起来像这样(针对这个问题精简到最低限度):
class Translation < ActiveRecord::Base
has_many :array_resources
end
class ArrayResource < ActiveRecord::Base
attr_accessible :array_items
has_many :array_items
accepts_nested_attributes_for :array_items
end
现在,在我的 Translation
模型中,我有一个调用 array_resources.build(params)
的方法,其中 params
是一个数组哈希值,其中每个哈希值还包含一个 :array_items
键,映射到另一个哈希值数组。
不幸的是,我收到以下错误:
ActiveRecord::AssociationTypeMismatch in ProjectsController#create
ArrayItem(#69835262797660) expected, got Hash(#18675480)
我读到的所有其他答案都谈到使用 accepts_nested_attributes_for
,但我已经这样做了。帮忙?
最佳答案
您不应直接分配给嵌套属性 array_items
,而应分配给 array_items_attributes
。
您应该使 :array_items_attributes
可访问:
class ArrayResource < ActiveRecord::Base
attr_accessible :array_items_attributes
然后在您的 params 哈希中使用键 :array_items_attributes
而不是 :array_items
。
关于ruby-on-rails - 批量作业 2 级深?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10726197/