我的模型Projelement
有这些回调:
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
所以当创建Projelement
时,一切都很好:
Activity
类型created
记录
但是当更新Projelement
时,我得到:
Activity
类型updated
条记录
可以在某处调用after_update
回调两次吗?
P.S。保存Projelement
时,它看起来像这样(Milestone
是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
答案 0 :(得分:2)
感谢@ frederick-cheung和@pjumble:
在update
方法中,Projelement
实例正在更新中:
update_attributes
被称为save
所以after_update
回调每Projelement