Rails

Interacting with Database Models in Rails Console

This guide explains how to perform basic database model operations using the Rails console. It provides an easy-to-understand guide for creating, selecting, updating, and deleting data.

Shou Arisaka
1 min read
Oct 19, 2025

This article explains how to interact with model databases using the Rails console (rails c).

Creating Data (Create/Insert)

Use the create method to create new data. This creates a model instance and saves it to the database in one operation.

Userinfo.create(name: "hoge")

Explanation: The create method executes in one step what would normally require creating an instance with the new method and saving it with the save method.

Selecting Data (Select)

To select data, use methods such as all or find.

# Get all records
Userinfo.all

# Get the name attribute of the record with ID 3
Userinfo.find(3).name
# => "hoge"

Note: The find method retrieves the record with the specified ID.

Updating Data (Update)

Use the update method to update data.

Userinfo.find(3).update(name: "fuga")

Explanation: Retrieve a specific record with the find method, then change its attribute values with the update method.

Deleting Data (Delete)

Use the delete or delete_all methods to delete data.

# Delete a specific record
Userinfo.find(3).delete

# Delete all records
Userinfo.delete_all

Note: The delete method deletes only the specified record, while the delete_all method deletes all records.

Share this article

Shou Arisaka Oct 19, 2025

๐Ÿ”— Copy Links