gpt4 book ai didi

ruby-on-rails - 更新表单在多对多关系中根本不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 16:45:13 24 4
gpt4 key购买 nike

在我的应用中,配方和成分之间存在多对多关系,一切正常但需要更新。

当我更新食谱时,我可以更新数据库中与食谱表关联的任何值,但不会修改成分

这是配方模型

class Recipe < ActiveRecord::Base
after_create :save_implementos
after_create :save_ingredientes
has_many :HasImplemento, dependent: :destroy
has_many :HasIngrediente, dependent: :destroy


has_many :ingredientes, through: :HasIngrediente
has_many :implementos, through: :HasImplemento


#CUSTOM SETTER#
def ingredientes=(value)
@ingredientes = value
end

def implementos=(value)
@implementos = value
#raise @implementos.to_yaml
end


private

#Guarda los implemenos de una receta
def save_implementos
#raise self.id.to_yaml
@implementos.each do |implemento_id|
HasImplemento.create(implemento_id: implemento_id, recipe_id: self.id)
end
end

def save_ingredientes
#raise @ingredientes.to_yaml
@ingredientes.each do |ingrediente_id|
HasIngrediente.create(ingrediente_id: ingrediente_id, recipe_id: self.id)
end

end

这是配料模型

class Ingrediente < ActiveRecord::Base
has_many :has_ingredientes
has_many :recipes, through: :HasIngrediente
end

这是连接表

class HasIngrediente < ActiveRecord::Base
belongs_to :recipe
belongs_to :ingrediente
end

最佳答案

我想你只是忘记了 Recipe 模型中的 accepts_nested_attributes_for:

accepts_nested_attributes_for :ingredients

关于ruby-on-rails - 更新表单在多对多关系中根本不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34671874/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com