0

I am new to active-record and I am doing table migrations. I want to see tables that I have migrated with their corresponding column names on the terminal. Does anyone know the command to do this?

Let say I have 2 tables cats and dogs, is there a command that would show something like this in my CLI?

dogs
---------
name 
breed
age

cats 
---------
name
breed
age
Kenkuts
  • 59
  • 1
  • 11

3 Answers3

1

There's a couple ways you can get this information about your database out but this should work from a rails console:

ActiveRecord::Base.connection.tables.each do |table|
  next unless ActiveRecord.const_defined?(table.classify) && !table.match(/schema_migrations/)
  puts table.classify.to_s
  puts '-----'
  puts table.classify.constantize.column_names
  puts
  puts
end;
Jay Dorsey
  • 3,437
  • 2
  • 16
  • 22
0

You are probably seeking:

ActiveRecord::Base.connection.tables

and

ActiveRecord::Base.connection.table_structure("projects")

Ref Rails: How to list database tables/objects using the Rails console?

noraj
  • 2,818
  • 1
  • 24
  • 34
0

If your goal is just to see the table structure on the command line, you could print the contents of db/schema.rb which Rails maintains as you run your migrations. The file includes table names, along with column names and data types. It even includes indices.

On macOS or Linux: cat db/schema.rb

On Windows: type db/schema.rb

aridlehoover
  • 2,539
  • 1
  • 23
  • 20