Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Update model attribute without invoking callbacks

I have a User model that has a :credits attribute. I want a simple button that will add 5 to the user's credits, through a route called "add" so that /users/3/add would add 5 to the credits of user id = 3.

def add     @user = User.find(params[:id])     @user.credits += 5     redirect_to root_path end 

That is the relevant part of my controller. The problem is, I dont want to call @user.save because I have a before_save callback that re-encrypts the user's password based on the current UTC time. I just want to simply add 5 to the attribute and avoid the callback, I never thought such a simple thing could be so hard.

EDIT:

I changed the callback to :before_create, here is my new controller code (relevant part):

  def add     @user = User.find(params[:id])     @user.add_credits(5)     @user.save     flash[:success] = "Credits added!"     redirect_to root_path   end 

and here is my code in the model:

 def add_credits(num)     self.credits = num  end 

EDIT 2:

Ok it was a validation problem that made the changes in "EDIT" not work, but I'd still love an answer to the original question of updating without callbacks!

like image 702
Sam Stern Avatar asked Dec 28 '11 01:12

Sam Stern


People also ask

Does Update_attributes Skip callbacks?

To skip validations on the update, use update_attribute that ensures: Validations are skipped. Callbacks are invoked. updated_at / updated_on column is updated (if that column is available)

Does Update_all trigger callbacks?

permalink #update_all(updates) ⇒ Object Updates all records in the current relation with details given. This method constructs a single SQL UPDATE statement and sends it straight to the database. It does not instantiate the involved models and it does not trigger Active Record callbacks or validations.

What is Active Record in Ruby on Rails?

1 What is Active Record? Active Record is the M in MVC - the model - which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database.

What is around callback in Rails?

In Rails, callbacks are hooks provided by Active Record that allow methods to run before or after a create, update, or destroy action occurs to an object. Since it can be hard to remember all of them and what they do, here is a quick reference for all current Rails 5 Active Record callbacks.


2 Answers

Rails 3.1 introduced update_column, which is the same as update_attribute, but without triggering validations or callbacks:

http://apidock.com/rails/ActiveRecord/Persistence/update_column

like image 87
cvshepherd Avatar answered Sep 25 '22 20:09

cvshepherd


As a general answer, in Rails 4 this is a simple way to update attributes without triggering callbacks:

@user.update_column :credits, 5 

If you need to update multiple attributes without triggering callbacks:

@user.update_columns credits: 5, bankrupt: false   

There are other options here in the Rails Guides if you prefer, but I found this way to be the easiest.

like image 45
Matt Avatar answered Sep 22 '22 20:09

Matt