My model Projelement
has these callbacks:
after_create { |p| p.log_activity "created" }
after_update { |p| p.log_activity "updated" }
And Projelement.log_activity
looks like:
# 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
So when a Projelement
is created, all's good:
- One
Activity
record ofcreated
type
But when a Projelement
is updated, then I get:
- Two duplicate
Activity
records ofupdated
type - Same timestamps
Can after_update
callback be called twice somewhere?
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
Thanks to @frederick-cheung and @pjumble:
In the
update
method, whereProjelement
instance is being updated:update_attributes
is being calledsave
is calledso the
after_update
callback is initiated twice perProjelement