[英]Rails 4 creates duplicates when updating nested attributes
I already tried whitelisting id, it doesn't help, it creates duplicates no matter what. 我已经尝试过将ID列入白名单,这无济于事,无论如何都会创建重复项。
Model: 模型:
accepts_nested_attributes_for :address, :social, :contact, :talent_parameter
The attributes I pass: 我通过的属性:
model_attributes = {
talent_parameter_attributes: {
},
contact_attributes: {
agency_link: base_url + href
},
social_attributes: {
},
address_attributes: {
}
}
update_model(model, model_attributes)
The permissions I set: 我设置的权限:
def self.update_model(model, attrs)
params = ActionController::Parameters.new(model: attrs)
model_params = params.require(:model)
model_params = model_params.permit(
:company,
:age,
:avatar,
:gender,
:contact_id,
talent_features: [],
talent_parameter_attributes: [:id, :weight_lbs, :dress, :shoe, :chest, :waist, :hips, :height_ft],
contact_attributes: [:id, :agency_link],
social_attributes: [:id]
)
model.update(model_params)
end
I don't understand. 我不明白 Each time it creates another copy of talent_parameter
, contact
, social
and address
. 每次创建talent_parameter
, contact
, social
和address
另一个副本时。 What can be wrong with it? 这有什么问题呢?
Did you include id for models to be updated ? 您是否包含要更新的模型的ID ? In Rails API , it states "For each hash that does not have an id key a new record will be instantiated"... 在Rails API中 ,它指出“对于没有ID键的每个哈希,将实例化一条新记录”。
So try this: 所以试试这个:
model_attributes = {
talent_parameter_attributes: {
},
contact_attributes: {
id: 7,
agency_link: base_url + href
},
social_attributes: {
},
address_attributes: {
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.