Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Tuesday, April 26, 2016

Rails: Update model attribute without invoking callbacks

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!

Answer by Finbarr for Rails: Update model attribute without invoking callbacks


Maybe your other before_save hook should check if the user's password has actually changed before encrypting it again.

Answer by Dave Newton for Rails: Update model attribute without invoking callbacks


You have a number of options, including changing which callback you use, e.g., after_create.

You can update columns without triggering callbacks, see Skipping Callbacks in the AR guide. For example, update_column doesn't trigger callbacks. The previous link lists non-triggering functions.

You could also use any of the Conditional Callback forms (or even an observer) for when the password is changed. See ActiveModel::Dirty, e.g., @user.password_changed?.

Answer by miked for Rails: Update model attribute without invoking callbacks


You should be able to use update_all to avoid triggering callbacks.

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

I'd prefer to put this logic in the model, but this should work to solve your original problem as spec'd in the controller.

Answer by DanneManne for Rails: Update model attribute without invoking callbacks


I think you should use the method update_counters in this case. Use it like this in your controller action:

def add    User.update_counters params[:id], :credits => 5    redirect_to root_path  end  

Answer by cvshepherd for Rails: Update model attribute without invoking callbacks


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

Answer by Matt Smith for Rails: Update model attribute without invoking callbacks


To update multiple attributes without callbacks you can use update_all in your model as so:

self.class.update_all({name: value, name: value}, self.class.primary_key => id)  

If you really want you can even try even a update_columns method and mixin this to your active record base class.

To update one attribute you can use update_column. In addition there is some specific methods that can found in the rails guides http://guides.rubyonrails.org/active_record_callbacks.html#skipping-callbacks

Answer by katzmopolitan for Rails: Update model attribute without invoking callbacks


A few options for how to do this in rails4 http://edgeguides.rubyonrails.org/active_record_callbacks.html#skipping-callbacks

Answer by hb922 for Rails: Update model attribute without invoking callbacks


For mongoid, I ended up using http://mongoid.org/en/mongoid/docs/persistence.html Specifically, you can use:

person.set(name:"Robert Pulson")  

and no callback will be issued. So cool.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

Related Posts:

0 comments:

Post a Comment

Popular Posts

Fun Page

Powered by Blogger.