saving an ActiveRecord without trigging the callbacks

149 views
Skip to first unread message

Christopher J. Bottaro

unread,
Mar 11, 2008, 7:43:27 PM3/11/08
to Ruby on Rails: Talk
Hello,
How can I save an ActiveRecord without trigger before_save,
after_save, etc?

Thanks for the help.

nas

unread,
Mar 12, 2008, 10:53:26 AM3/12/08
to Ruby on Rails: Talk
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" <cjbott...@gmail.com>
wrote:

AndyV

unread,
Mar 12, 2008, 12:18:05 PM3/12/08
to Ruby on Rails: Talk
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" <cjbott...@gmail.com>
wrote:

Christopher J. Bottaro

unread,
Mar 12, 2008, 5:39:23 PM3/12/08
to Ruby on Rails: Talk
Excellent, thanks. I am just updating a single field.

Christopher J. Bottaro

unread,
Mar 12, 2008, 5:47:51 PM3/12/08
to Ruby on Rails: Talk
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" <cjbott...@gmail.com>

jacques

unread,
Apr 2, 2008, 2:13:59 AM4/2/08
to Ruby on Rails: Talk
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" <cjbott...@gmail.com>

Julian Leviston

unread,
Apr 2, 2008, 2:57:57 AM4/2/08
to rubyonra...@googlegroups.com
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/

Reply all
Reply to author
Forward
0 new messages