High Leverage Rails
Introduction
Introduction to this course
Why use Ruby on Rails
Why use SQLite
Ruby on Rails + SQLite
Powering Your App with SQLite
Creating tables
Timestamps
Column types
Typeof
Ruby types
Creating table introduction
Creating table advanced
Inserting data
Updating data
Upserting data
Reading data
Virtual columns
Enums
Introduction to JSON
Indexing JSON
JSON table functions
Building a Modern Rails Application
Creating a new Rails application
Installing Solid Queue
Installing Solid Cache
Installing Solid Cable
Dockerfile
Application overview
Authentication
Base styles
Registration
Scaffolding posts
Polishing posts
Scaffolding comments
Polishing comments
Polishing models
Polishing controllers
Creating new post
Updating post
Reviewing MVP
Tagging posts
Custom tags
Friendly URLs
Full text search
Deploying & Operating Your App
Backups
Check Litestream locally
Verifying backups
Deployment options
Deploying with Hatchbox
Deployment constraints
Vertical scaling
Database access
Migrations
Locked video

Please purchase the course to watch this video.

Video thumbnail
Powering Your App with SQLite
Updating data

Full Course

$
129
$179
USD, one-time fee
This course came at the perfect time. I’ve recently gotten back into Rails after an 18-year hiatus, and this was a perfect refresher and shows just how much you can accomplish with Rails right out of the box.
Garrett Winder
Garrett Winder

Rails hosting made simple

Deploy apps to servers that you own and control.

Move fast and fix things

Application monitoring that helps developers get it done.

Summary

Explore different Active Record update methods in Rails, including update, update_columns, and save, and learn how they impact validations, callbacks, and timestamps. Understand when to use each method to balance data integrity and performance in your Rails applications.

Video Transcript

After creating records, how can you update specific values? Let’s jump back into our sandbox and explore that together.

We have our posts table with title and body columns, and I’ve set up our Post model with a validation and a callback. I’ve also created an initial post with title: "title one" and "some text" for the body. Now, let’s clear everything and start working with updates.

Using the update Method

The most common way to update a particular value is by calling the update! method, passing the column name and a new value:

post.update!(title: "updated")

This method:

  • Wraps the update query in a transaction.
  • Uses a generic prepared SQL statement for efficiency and security.
  • Automatically updates the updated_at timestamp.

Examining the SQL query, we see:

UPDATE posts SET title = ?, updated_at = ? WHERE posts.id = ?

This ensures that every update follows the same structure, improving performance and preventing SQL injection.

Handling Validation Errors with update

If we try to update the title to an empty value, our validation rule kicks in:

post.update!(title: nil)
# => ActiveRecord::RecordInvalid: Validation failed: Title can't be blank

Using update without the bang (!), we get false instead of an exception:

post.update(title: nil)
# => false
post.errors
# => [...Error attribute=title, type=blank,...]

This allows us to choose how to handle errors—whether to catch them explicitly with update! or check them manually with update.

Bypassing Validations and Callbacks with update_columns

What if we need to update a record without triggering validations or callbacks? We can use update_columns:

post.update_columns(title: nil)

Key differences:

  • Does not check validations—so even if the title is required, this update still executes.
  • Does not trigger callbacks, meaning any after-update logic will not run.
  • Does not update the updated_at timestamp automatically.

Checking the database:

post.reload.title
# => nil (Title is now empty, even though validations would normally prevent this)

Because update_columns skips important Rails mechanisms, use it carefully—only when you’re sure it’s necessary.

Updating Values with save

Much like new, you can also update an attribute directly on a post instance and call save:

post.title = "updated again"
post.save!
  • Runs validations before saving.
  • Wraps the update in a transaction.
  • Automatically updates updated_at.

Using save! ensures an exception is raised on failure, while save returns false if the update is invalid:

post.title = nil
post.save
# => false
post.errors.full_messages
# => [...Error attribute=title, type=blank,...]

Choosing the Right Update Method

Method Runs Validations? Runs Callbacks? Updates updated_at? Best Use Case
update! ✅ Yes ✅ Yes ✅ Yes Default method for updates
update ✅ Yes ✅ Yes ✅ Yes When you want to check errors manually
update_columns ❌ No ❌ No ❌ No Skipping validations and callbacks for performance-sensitive updates
save! ✅ Yes ✅ Yes ✅ Yes Ensures an exception on failure
save ✅ Yes ✅ Yes ✅ Yes When updating an instance directly

In the next video, we’ll explore a more advanced technique—updating or inserting records in a single SQL query for even greater efficiency.