
- Ruby on Rails - Home
- Ruby on Rails - Introduction
- Ruby on Rails - Installation
- Ruby on Rails - IDEs
- Ruby on Rails - Hello World
- Ruby on Rails - Framework
- Ruby on Rails - Directory Structure
- Ruby on Rails - Rails Console
- Ruby on Rails - Bundler
- Ruby on Rails - Examples
- Ruby on Rails - Database Setup
- Ruby on Rails - Active Records
- Ruby on Rails - Validation
- Active Record Associations
- Active Record Query
- Ruby on Rails - Migrations
- Ruby on Rails - Active Model
- Ruby on Rails - Controllers
- Cookies and Session
- Ruby on Rails - Authentication
- Ruby on Rails - Routes
- Ruby on Rails - Views
- Ruby on Rails - Rendering
- Ruby on Rails - Layouts
- Ruby on Rails - Scaffolding
- Ruby on Rails - Forms
- Ruby on Rails - Active Jobs
- Ruby on Rails - Action Text
- Ruby on Rails - Active Storage
- Ruby on Rails - JavaScript
- Ruby on Rails - Propshaft
- Ruby on Rails - ImportMap
- Ruby on Rails - AJAX
- Ruby on Rails - WebSockets
- Ruby on Rails - Action Cable
- Ruby on Rails - File Uploading
- Ruby on Rails - Send Emails
- Ruby on Rails - Rack
- Ruby on Rails - Error Handling
- Ruby on Rails - Deployment
- Ruby on Rails Resources
- Ruby on Rails - References Guide
- Ruby on Rails - Quick Guide
- Ruby on Rails - Resources
- Ruby on Rails - Discussion
- Ruby Tutorial
- Ruby Tutorial
Ruby on Rails Console
Rails API provides a useful feature called Console. The console is an interactive tool for testing the code in our Rails application.
Before opening a console session, let us define a Person model with name and age columns.
rails generate model Person name:string age: integer invoke active_record create db/migrate/20250227112053_create_people.rb create app/models/person.rb invoke test_unit create test/models/person_test.rb create test/fixtures/people.yml
This will create Person class derived from ApplicationRecord:
class Person < ApplicationRecord end
As in the previous chapter, run the migration on this model.
PS C:\Users\mlath\RubymineProjects\library> rails db:migrate == 20250227112053 CreatePeople: migrating ===================================== -- create_table(:people) -> 0.0230s == 20250227112053 CreatePeople: migrated (0.0247s) ============================ == 20250227112941 Persons: migrating ========================================== == 20250227112941 Persons: migrated (0.0000s) =================================
Open a PowerShell or Command prompt if using Windows, or a terminal if on Ubuntu. Make sure that you are logged into the project directory (library in this case).
PS C:\Users\mlath\RubymineProjects\library> rails console Loading development environment (Rails 8.0.1) library(dev)>
You can check the version of Rails installation in the console window.
library(dev)> Rails.version => "8.0.1"
All the resources of the project, including the Person model can be accessed in the console. To get the list of columns defined in Person model, use column_names property.
library(dev)> Person.column_names => ["id", "name", "age", "created_at", "updated_at"]
Note that Rails adds created_at and updated_at columns.
The new() function of the Person class creates a new Person object.
library(dev)> p1 = Person.new(name:"Ravi", age:21) => #<Person:0x000002ac6815e228 id: nil, name: "Ravi", age: 21, created_at: nil, updated_at: nil>
The object is placed in the memory. To save it persistently in the database, call the save() function.
library(dev)> p1.save TRANSACTION (0.6ms) BEGIN /*application='Library'*/ TRANSACTION (0.7ms) BEGIN /*application='Library'*/ Person Create (4.6ms) INSERT INTO "people" ("name", "age", "created_at", "updated_at") VALUES ('Ravi', 21, '2025-02-27 12:29:12.006534', '2025-02-27 12:29:12.006534') RETURNING "id" /*application='Library'*/ TRANSACTION (1.5ms) COMMIT /*application='Library'*/ => true
The auto-created fields created_at and updated_at bear the time stamp:
library(dev)> p1 => #<Person:0x000002ac6815e228 id: 1, name: "Ravi", age: 21, created_at: "2025-02-27 12:29:12.006534000 +0000", updated_at: "2025-02-27 12:29:12.006534000 +0000">
The create() function creates a new object and save it in the database.
library(dev)> Person.create(name:"Raja", age:25) TRANSACTION (1.0ms) BEGIN /*application='Library'*/ Person Create (2.8ms) INSERT INTO "people" ("name", "age", "created_at", "updated_at") VALUES ('Raja', 25, '2025-02-27 12:30:33.474115', '2025-02-27 12:30:33.474115') RETURNING "id" /*application='Library'*/ TRANSACTION (39.1ms) COMMIT /*application='Library'*/ => #<Person:0x000002ac690d6908 id: 2, name: "Raja", age: 25, created_at: "2025-02-27 12:30:33.474115000 +0000", updated_at: "2025-02-27 12:30:33.474115000 +0000">
The all() function retrieves all the records in the underlying table.
library(dev)> Person.all Person Load (1.0ms) SELECT "people".* FROM "people" /* loading for pp */ LIMIT 11 /*application='Library'*/ => [#<Person:0x000002ac690dc588 id: 1, name: "Ravi", age: 21, created_at: "2025-02-27 12:29:12.006534000 +0000", updated_at: "2025-02-27 12:29:12.006534000 +0000">, #<Person:0x000002ac690dc308 id: 2, name: "Raja", age: 25, created_at: "2025-02-27 12:30:33.474115000 +0000", updated_at: "2025-02-27 12:30:33.474115000 +0000">]
Rails also adds id as the primary ley for the model. You can fetch a given record by calling the find() function and giving id as an argument.
To find the record with id=1 −
library(dev)> p1=Person.find(1) Person Load (0.9ms) SELECT "people".* FROM "people" WHERE "people"."id" = 1 LIMIT 1 /*application='Library'*/ => #<Person:0x000002ac690db048 ...
To modify a record, assign a new value to one or more attributes (columns) and call save() function. Let us change the name of person with id=1 to Ravindra.
library(dev)> p1.name="Ravindra" => "Ravindra" library(dev)> p1.save TRANSACTION (0.6ms) BEGIN /*application='Library'*/ Person Update (4.2ms) UPDATE "people" SET "name" = 'Ravindra', "updated_at" = '2025-02-27 12:34:14.306655' WHERE "people"."id" = 1 /*application='Library'*/ TRANSACTION (38.3ms) COMMIT /*application='Library'*/ => true
Note that Rails translates this function call to the equivalent UPDATE query.
You can apply filter to conditionally fetch records from the underlying table. You can use all the comparison operators in the where() function.
library(dev)> Person.where("age > ?", 25) Person Load (1.7ms) SELECT "people".* FROM "people" WHERE (age > 25) /* loading for pp */ LIMIT 11 /*application='Library'*/ => [#<Person:0x000002ac6993ac98 id: 2, name: "Raja", age: 30, created_at: "2025-02-27 12:30:33.474115000 +0000", updated_at: "2025-02-27 12:36:04.560314000 +0000">]
Here also Rails internally executes an equivalent SELECT query .
The update() function works as an equivalent foe assigning new value to a column and calling save() function. Let us update the name of a record with id=2.
library(dev)> p1.update(name:"Rajendra") TRANSACTION (0.6ms) BEGIN /*application='Library'*/ Person Update (2.9ms) UPDATE "people" SET "name" = 'Rajendra', "updated_at" = '2025-02-27 12:38:06.880558' WHERE "people"."id" = 2 /*application='Library'*/ TRANSACTION (38.8ms) COMMIT /*application='Library'*/ => true
How can you delete a record from the table? Let us first add a new record , Rails assigns a unique id to it.
library(dev)> Person.create(name:"Roger", age:20) TRANSACTION (0.9ms) BEGIN /*application='Library'*/ Person Create (2.9ms) INSERT INTO "people" ("name", "age", "created_at", "updated_at") VALUES ('Roger', 20, '2025-02-27 12:40:00.051918', '2025-02-27 12:40:00.051918') RETURNING "id" /*application='Library'*/ TRANSACTION (39.3ms) COMMIT /*application='Library'*/ => #<Person:0x000002ac69935f18 id: 3, name: "Roger", age: 20, created_at: "2025-02-27 12:40:00.051918000 +0000", updated_at: "2025-02-27 12:40:00.051918000 +0000"> library(dev)> p1 = Person.find(3) Person Load (0.9ms) SELECT "people".* FROM "people" WHERE "people"."id" = 3 LIMIT 1 /*application='Library'*/ => #<Person:0x000002ac6993e9d8 ...
To delete an instance of the Person model, call the destroy function.
library(dev)> p1.destroy TRANSACTION (0.6ms) BEGIN /*application='Library'*/ Person Destroy (2.0ms) DELETE FROM "people" WHERE "people"."id" = 3 /*application='Library'*/ TRANSACTION (39.6ms) COMMIT /*application='Library'*/ => #<Person:0x000002ac6993e9d8 id: 3, name: "Roger", age: 20, created_at: "2025-02-27 12:40:00.051918000 +0000", updated_at: "2025-02-27 12:40:00.051918000 +0000">
This executes the DELETE query as can be seen from the console log.
Most of the actions required to create and manage a Rails application are performed using various commands. For example Rails new creates the directory structure for a new application, Rails generate allows you to generate components such as ActiveRecord and migrations.
Here is the list of Rails commands:
- bin/rails console
- bin/rails server
- bin/rails test
- bin/rails generate
- bin/rails db:migrate
- bin/rails db:create
- bin/rails routes
- bin/rails dbconsole
- rails new app_name
The dbconsole is also another useful console command. It opens the console of the corresponding database which you have used as a backend for your application. In this case, the library application has been created with PostgreSQL as the backend. Hence, dbconsole opens the psql console.
C:\Users\mlath\RubymineProjects\library>rails dbconsole Password for user postgres: psql (16.2) WARNING: Console code page (437) differs from Windows code page (1252) 8-bit characters might not work correctly. See psql reference page "Notes for Windows users" for details. Type "help" for help. library_development=#