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
http://nasir.wordpress.com/2007/12/03/stored-procedures-and-rails/

On Mar 11, 11:43 pm, “Christopher J. Bottaro” [email protected]

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.

On Mar 11, 7:43 pm, “Christopher J. Bottaro” [email protected]

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

Ok… so no answers to this one?

I understand find_by_sql… whats the update_by_sql alternative?

On Mar 12, 2:47 pm, “Christopher J. Bottaro” [email protected]

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

On Mar 12, 4:39 pm, “Christopher J. Bottaro” [email protected]

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/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs