其中是after_update调用两次? [英] where is after_update called twice?

查看:242
本文介绍了其中是after_update调用两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型 Projolement 有这些回调:

after_create   { |p| p.log_activity "created" }
after_update   { |p| p.log_activity "updated" }

Projelement.log_activity 看起来像:

# 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

因此,当创建 Projolement 时,所有的都是好的:

So when a Projelement is created, all's good:


  • 一个活动 创建的记录类型

  • One Activity record of created type

但是当 Projelement 更新时,我会得到:

But when a Projelement is updated, then I get:


  • 两个重复活动 更新的记录类型

  • 相同时间戳

  • Two duplicate Activity records of updated type
  • Same timestamps

可以 after_update 回调在某处被调用两次?

Can after_update callback be called twice somewhere?

ps当保存 Projelement 时,它看起来像这样( Milestone Projolement ):

p.s. when a Projelement is saved, it looks something like this (a Milestone is a Projelement):

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


推荐答案

感谢@ frederick-cheung和@pjumble:

Thanks to @frederick-cheung and @pjumble:

更新方法,其中 Projolement 实例正在更新:

In the update method, where Projelement instance is being updated:


  • update_attributes 正在呼叫

  • 然后 save 调用

  • update_attributes is being called
  • Then save is called

,因此 after_update 回调每< c $ c> Projolement

这篇关于其中是after_update调用两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆