saving an ActiveRecord without trigging the callbacks

Hello, How can I save an ActiveRecord without trigger before_save, after_save, etc?

Thanks for the help.

You can skip callbacks while deleting records but I don't think you can while saving them, hence you have to rely on raw SQL instead of RoR AR / ORM. Check this out, you might get some idea from it

It depends on the type of update you want to do. If you only need to update a single parameter or two then you can use update_attribute (note: singular). As the name suggests it accepts a single attribute name and the corresponding value.

If you need to do more than that then you may want to reconsider what your callbacks are doing. At the very least you could add conditions to the top of the callback that causes them to shortcircuit.

Excellent, thanks. I am just updating a single field.

Oops, spoke too soon. ActiveRecord::Base#update_attribute does indeed trigger the before/after save callbacks. I don't think it triggers validation though.

-- C

Ok.. so no answers to this one?

I understand find_by_sql... whats the update_by_sql alternative?

You can put whatever SQL you like in find_by_sql, I think. Therefore,
you can put an update, or delete or anything in there.

Hope that helps.

Julian.

Learn Ruby on Rails! CHECK OUT THE FREE VIDS (LIMITED TIME) NEW VIDEO
OUT 3rd APRIL http://sensei.zenunit.com/