gpt4 book ai didi

ruby-on-rails - 调用实例方法时更新记录的正确方法。

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

当我调用更新时,我不想只是像我想的那样更新记录:

  1. 调用模型函数来更新模型的属性。
  2. 更新模型。

--

def update
@simulation = Simulation.find(params[:id])
@simulation.next # This is a function that has some logic to changes a property of the simulation record
@simulation.update(simulation_params)
end

这是解决此问题的正确方法,还是我应该使用单独的 Controller 功能或其他路线?

最佳答案

为了清楚起见,我个人会在 Simulation 中创建一个实例方法,高级代码应该类似于..

#Simulation model
class Simulation

....

def next_and_update(attrs)
next
update(attrs)
end

end

#controller
def update
@simulation = Simulation.find(params[:id])
@simulation.next_and_update(simulation_params)
end

想法是,如果您可以阅读代码并理解发生了什么,那么多 1-2 行就可以了。

关于ruby-on-rails - 调用实例方法时更新记录的正确方法。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31908618/

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