Dumping the structure of one table in the Rails console
- Name
- Dennis Paagman
- @djfpaagman
Recently I ran into a little issue where I was not sure if a migration went through correctly (why is completely different story).
To check the structure of the table on the production database I wanted some output similar to schema.rb, but directly from the console.
Fortunately the code that dumps the table is a neat little class that dumps to STDOUT by default.
Simply run the following command in the Rails console:
ActiveRecord::SchemaDumper.dump
This dumps out the whole structure of your database, though. It also has a nice feature to ignore certain tables. Combining this with the Rails command to list all tables gets us where we want:
ignore_tables = ActiveRecord::Base.connection.tables - ["your_table"]
ActiveRecord::SchemaDumper.ignore_tables = ignore_tables
ActiveRecord::SchemaDumper.dump
Simply run the dump command again and you’ll only get the output for that specific table.
I've got some availability for new clients! Want to move faster on your Rails app, migrate that old legacy system, or agent-proof your development setup? With 15 years of Rails experience and building with AI agents every day, I could be a great fit.
Get in touch