Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ror
ruby

What is update in ROR?

Behzad Ahmad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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)

RELATED TAGS

ror
ruby

CONTRIBUTOR

Behzad Ahmad
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring