mongoid/mongoid — Issue #1742: before_destroy callback not working once destroying embedded docs
Author: zakwanhaj
Created: 2012-02-22 09:22:16+00:00
State: open

HI,
I have encountered a wired issue. I have the following model:

class Answer
include Mongoid::Document
field :score, :type => Float, :default => 0.0
embeds_many :points
end

And the point class is:

class Point
include Mongoid::Document

after_create :change_score
before_destroy :change_score # or after_destory

field :vote, :type => Float, :default => 1.0
belongs_to :user
embedded_in :answer, :inverse_of => :t_points

def change_score
logger.debug "aa"
self.answer.inc(:score, self.vote)
logger.debug "bb"
end

after_create callback is working fine:
>> @answer.points.create(:user => @user, :vote => -10.0)

it gives me in log file:
____
aa
MONGODB project_development['answer'].update({"_id"=>BSON::ObjectId('4d3f72eb0517b527b6001f00')}, {"$inc"=>{"score"=>-10.0}})
bb
____

but when destroying a 'point' such like:
>> @answer.points.destroy_all(:user_id => @user.id)
the command in before_destroy method not fired !!
____
aa
bb
____

then I checked the score in Answer model
>> @answer.score ..... is not changed !!

I used :cascade_callbacks but I got same problem as well
Appreciate helping me.. thanks