Translate

April 18, 2012

Difference between update_attribute and update_column


We use update_attribute most of the time, update_column serves same purpose, there is below difference
  • update_attribute
  • It updates the single attribute with bypassing the validations.
  • update_column
  • It updates the single attribute with bypassing the validations as well as the callbacks.

Syntax of both are as follow:
Model.update_attribute :field, 'value'
Model.update_column :field, 'value'

update_column is added in the rails 3.2.1

In case if you need to use update_column in lower Rails version then you can apply simple patch given below.

module ActiveRecord
  module AttributeMethods
    def update_column(name, value)
      name = name.to_s
      self.class.update_all({ name => value }, self.class.primary_key => id) == 1
     end
   end
end

Related posts:
Difference between update_attribute and update_column 
What is different in update_all, update,  update_attribute, update_attributes methods of Active Record
Difference between .nil?, .empty?, .blank?, .present? 
render vs redirect_to in Ruby on Rails

About

April 17, 2012

What is different in update_all, update, update_attribute, update_attributes methods of Active Record

We use Active Record update* methods regularly, following text explains the difference between all update* the methods

update(id,attributes)
Update single or multiple objects using update method of active record. When update method is called it invokes model based validation, and save the object when validation passes successfully else object is not save.
It accepts two arguments, id and attributes which need to update.
User.update(1,:first_name => "John",:last_name => "Turner")
same way you are able to update multiple objects.
User.update([1,2],[{:first_name => "John",:last_name => "Turner"},{:department => "admin"}])

update_all(attribute, conditions, options)
It updates the given attribute of object by invoking specified conditions and options like order, limit. This will not invoke validation rules, methods of the model.
User.update_all("department = admin, "user_role Like '%admin'",:limit => 10)

update_attribute
This method update a single attribute of object without invoking model based validation.
user = User.find_by_id(params[:id])
user.update_attribute :role,"admin_manager"

update_attributes
This method update multiple attribute of single object and also pass model based validation.
attributes = {:first_name => "Peter", :age => 30}
user = User.find_by_id(params[:id])
user.update_attributes(attributes)

I think this article helped you to understand different Active Record update* methods. And which method to use in specific situation.
If have any query or suggestion than simply leave the comment.

Related posts:
Difference between update_attribute and update_column 
What is different in update_all, update,  update_attribute, update_attributes methods of Active Record
Difference between .nil?, .empty?, .blank?, .present? 
render vs redirect_to in Ruby on Rails

About