Rails: How to list database tables/objects using the Rails console?
Categories:
Rails: How to List Database Tables/Objects Using the Rails Console

Learn various methods to inspect your database schema and list tables, views, and other objects directly from the Rails console, enhancing your development workflow.
When working with a Ruby on Rails application, understanding your database schema is fundamental. The Rails console provides a powerful interface to interact with your application's environment, including its database. This article will guide you through several effective ways to list database tables and other objects directly from the Rails console, catering to different database adapters and levels of detail you might need.
Using ActiveRecord's Connection Adapter
ActiveRecord, Rails' ORM, provides a database-agnostic way to interact with your database. The connection adapter exposes methods to query schema information. This is often the most reliable and portable method across different database systems (PostgreSQL, MySQL, SQLite, etc.).
ActiveRecord::Base.connection.tables
Listing all tables using ActiveRecord's connection adapter.
This command returns an array of strings, where each string is the name of a table in your database. It's a quick and clean way to get a list of all tables managed by ActiveRecord.
ActiveRecord::Base.connection
object is your gateway to low-level database interactions. You can explore its methods using ActiveRecord::Base.connection.methods.sort
for more advanced queries.Inspecting Database Views and Other Objects
Beyond just tables, databases often contain views, sequences, and other objects. While ActiveRecord::Base.connection.tables
focuses on tables, you might need to query for other object types, especially in more complex database setups or when dealing with legacy systems. The exact method can vary slightly depending on your database adapter.
# For PostgreSQL (example for views)
ActiveRecord::Base.connection.views
# For PostgreSQL (example for sequences)
ActiveRecord::Base.connection.execute("SELECT sequence_name FROM information_schema.sequences WHERE sequence_schema = 'public';").map { |r| r['sequence_name'] }
Listing views and sequences in PostgreSQL via ActiveRecord.
For databases like PostgreSQL, the connection adapter might offer specific methods like views
. If not, or for more granular control, you can always resort to executing raw SQL queries using ActiveRecord::Base.connection.execute
.
flowchart TD A[Start Rails Console] --> B{Need Tables Only?} B -->|Yes| C[ActiveRecord::Base.connection.tables] B -->|No| D{Need Views/Other Objects?} D -->|Yes, specific method exists| E[ActiveRecord::Base.connection.views] D -->|Yes, no specific method| F[ActiveRecord::Base.connection.execute("RAW SQL")] C --> G[Output: Array of Table Names] E --> G F --> G
Decision flow for listing database objects in Rails console.
Listing Tables with Schema Dumper (Development/Test)
While not directly listing from the live database connection, inspecting your db/schema.rb
file can also give you a comprehensive overview of your tables and their structure. This file is generated by Rails and reflects the current state of your database schema. It's particularly useful for understanding the schema without connecting to the database directly, or for reviewing changes.
cat db/schema.rb | grep 'create_table'
Using grep
to find table definitions in schema.rb
.
db/schema.rb
file is typically updated when you run migrations. Ensure your migrations are up-to-date (rails db:migrate
) for schema.rb
to accurately reflect your database structure.Practical Steps for Database Inspection
Here's a summary of the most common and useful commands to inspect your database from the Rails console.
1. Start the Rails Console
Open your terminal and navigate to your Rails project directory. Then, start the console in development mode (default) or a specific environment:
rails console
# or for production
rails console production
2. List All Tables
Once in the console, execute the following command to get an array of all table names:
ActiveRecord::Base.connection.tables
3. List Database Views (PostgreSQL Example)
If you are using PostgreSQL and have views, you can often list them directly:
ActiveRecord::Base.connection.views
For other databases or if views
is not available, you might need to use raw SQL.
4. Execute Raw SQL Queries
For any other database object or complex queries, you can execute raw SQL. For example, to list all tables and views in PostgreSQL:
ActiveRecord::Base.connection.execute("SELECT table_name, table_type FROM information_schema.tables WHERE table_schema = 'public';").to_a