Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show data in table from Rails Console for PostgreSQL

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?

like image 824
Jensky Avatar asked Sep 14 '14 15:09

Jensky


People also ask

How do I show data in PostgreSQL?

Use the \dt or \dt+ command in psql to show tables in a specific database. Use the SELECT statement to query table information from the pg_catalog.

How do I view a Rails database?

You can use rails dbconsole to view the database that your rails application is using. It's alternative answer rails db . Both commands will direct you the command line interface and will allow you to use that database query syntax.


1 Answers

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
like image 79
house9 Avatar answered Sep 30 '22 16:09

house9