Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "update_attributes" without executing "before_save"?

Tags:

I have a before_save in my Message model defined like this:

   class Message < ActiveRecord::Base      before_save lambda { foo(publisher); bar }    end 

When I do:

   my_message.update_attributes(:created_at => ...) 

foo and bar are executed.

Sometimes, I would like to update message's fields without executing foo and bar.

How could I update, for example, the created_at field (in the database) without executing foo and bar ?

like image 962
Misha Moroshko Avatar asked Aug 30 '11 13:08

Misha Moroshko


2 Answers

In rails 3.1 you will use update_column.

Otherwise:

In general way, the most elegant way to bypass callbacks is the following:

class Message < ActiveRecord::Base   cattr_accessor :skip_callbacks   before_save lambda { foo(publisher); bar }, :unless => :skip_callbacks # let's say you do not want this callback to be triggered when you perform batch operations end 

Then, you can do:

Message.skip_callbacks = true # for multiple records my_message.update_attributes(:created_at => ...) Message.skip_callbacks = false # reset 

Or, just for one record:

my_message.update_attributes(:created_at => ..., :skip_callbacks => true) 

If you need it specifically for a Time attribute, then touch will do the trick as mentioned by @lucapette .

like image 74
jbescoyez Avatar answered Oct 09 '22 09:10

jbescoyez


update_all won't trigger callbacks

my_message.update_all(:created_at => ...) # OR Message.update_all({:created_at => ...}, {:id => my_message.id}) 

http://apidock.com/rails/ActiveRecord/Base/update_all/class

like image 29
fl00r Avatar answered Oct 09 '22 10:10

fl00r