How to update a column directly in Database | Ruby on Rails

How to update a column directly in Database in Ruby on Rails using update_column and update_columns. This is useful when you don't want to invoice Rails validations, callbacks and want to push the data directly to database.

Posted by Ameena on 04 Sep 2017

In order to update a field there are many methods in Rails. But, to update a field in Database directly you should be using updatecolumn or updatecolumns.

update_column

update_column( :field_name, field_value )
  • It doesn't validate the record.
  • It doesn't call callbacks.
  • It doesn't call save method.

Thus, updates the record in Database directly by not going through regular update procedures.

update_columns

update_columns( field_name: field_value )
  • It doesn't validate the record.
  • It doesn't call callbacks.
  • It doesn't call save method.
  • It works for version above 3 in Rails.

Keep Coding !!!

Ameena


Have a Project in mind?