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

2 Answers2

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,563
  • 2
  • 18
  • 24
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
  • 3,964
  • 1
  • 30
  • 38