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.