What is update in ROR?

The update action updates the database record itself. If validations succeed, it updates the single or multiple records in the database. The object is returned regardless of if the object is successfully saved to the database.

Basic syntax

Code

Let’s look at a general example:

user = User.find_by(name: 'Behzad')
user.name = 'Ahmad'
user.save

In this code, we find the user named Behzad. Then, we update its name to Ahmad and save the data. We can also write it like this:

user = User.find_by(name: 'Behzad')
user.update(name: 'Ahmad')

In this code, we don’t need to save the data because we are using the update method; so, it automatically updates the data in the database.

We can update multiple records like this:

user = { 1 => { "firstName" => "Behzad" }, 2 => { "firstName" => "Mike" } }
Object1.update(user.keys, user.values)

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved