views:

53

answers:

2

My question is similar to this one http://stackoverflow.com/questions/1342761/how-to-skip-activerecord-callbacks but instead of AR I'm using Mongoid, It seems like that isn't implemented yet in the current version of Mongoid, so I'd like to know what should be an elegant solution to implement it. (if necessary).

+1  A: 

I search on the code. And there are no way to avoid callback in Mongoid. In both version 1.9 and 2.0.

You need made a patch or a feature request about that.

shingara
Thanks, Yes I was also reviewing the 2.0.0.beta and there is no skipping callbacks support ... let's see how AR does it and then implement it on my version of mongoid.
jpemberthy
+1  A: 

Yes you can!

Mongoid is built on ActiveModel and ActiveModel has a skip_callback function. You can use skip_callback like this:

# skip the callback
MyModelClass.skip_callback(:save, :before, :ensure_foo_is_not_bar)

my_model_instance.update_attributes :foo => 'bar'

# restore the callback for future calls
MyModelClass.set_callback(:save, :before, :ensure_foo_is_not_bar)

I'm using this without a hitch in a big app. For more info, see this blog post by Jeff Kreeftmeijer:

http://jeffkreeftmeijer.com/2010/disabling-activemodel-callbacks/

bowsersenior
Ah Cool, thanks!
jpemberthy