Ruby on rails 调用两次更新后在哪里?
My modelRuby on rails 调用两次更新后在哪里?,ruby-on-rails,callback,Ruby On Rails,Callback,My modelProjeElement具有以下回调: after_create { |p| p.log_activity "created" } after_update { |p| p.log_activity "updated" } 而Projelement.log\u活动看起来像: # create new Activity object # point it to the project & projelement @a = Activity.new @a.proje
ProjeElement
具有以下回调:
after_create { |p| p.log_activity "created" }
after_update { |p| p.log_activity "updated" }
而Projelement.log\u活动
看起来像:
# create new Activity object
# point it to the project & projelement
@a = Activity.new
@a.projelement = self
@a.project = self.project
# record the type of activity
case op_type
when "created"
@a.operation = "created"
when "deleted"
@a.operation = "deleted"
when "updated"
@a.operation = "updated"
end
@a.save
因此,当创建项目时,一切都很好:
- 创建的
类型的一条活动
记录
但是当更新项目时,我会得到:
- 两个重复的
活动
更新的类型记录
- 相同的时间戳
更新后的回调是否可以在某个地方调用两次
p、 当一个项目
被保存时,它看起来像这样(一个里程碑
是一个项目
):
def更新
@里程碑=里程碑。查找(参数[:id])
回应待办事项|格式|
如果@milestone.update_属性(参数[:milestone])
#标记当前用户具有此里程碑
@milestone.read_by.clear
@milestone.read_by感谢@frederick cheung和@pjumble:
在update
方法中,其中正在更新Projelement
实例:
- 正在调用更新属性
然后调用save
因此,更新后的回调将根据项目启动两次愚蠢的问题:您是否保存项目两次?更新代码是什么样子的?愚蠢的初始帖子:对不起,我忘记发布更新代码了。嗯,我以为我没有存两次钱(看了很久)。但是粘贴代码。。。我似乎会调用update\u attributes
,然后调用save
?将此作为评论发布,因为您确实回答了自己的问题。是的,这是因为你在打电话(内部调用save
),然后你自己又在打save
。谢谢伙计们:有时候只要“大声说出来”就能帮助你看清眼前的东西。
def update
@milestone = Milestone.find(params[:id])
respond_to do |format|
if @milestone.update_attributes(params[:milestone])
# flag that current_user has this milestone
@milestone.read_by.clear
@milestone.read_by << current_user
@milestone.save
...
end
end
end