6

I find something like this: Rails: How to list database tables/objects using the Rails console?

This line is fine:

ActiveRecord::Base.connection.tables

and returns all tables

but

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

generate error:

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

I think that

table_structure

is not Postgres method.

How can I list all data from the table in Rails console for Postgres database?

Community
  • 1
  • 1
Jensky
  • 919
  • 3
  • 12
  • 26

1 Answers1

13

You can get this information from postgres directly from the command line

psql your_development_database -c "\d"
-- lists all database tables

psql your_development_database -c "\d users"
-- lists all columns for a table; 'users' in this case

If you want to look at Model attributes in the rails console

User.new
User.new.inspect

# or install the awesome_print gem for better output
ap User.new
house9
  • 20,359
  • 8
  • 55
  • 61
  • don't forget: \q to exit the psql console. just saved you a search ;) – grooble Sep 09 '17 at 02:47
  • @grooble - the above is using the `-c` option so it executes the command but does not enter an interactive `psql` session, no need to `\q` in that case – house9 Sep 13 '17 at 19:07