在Rails控制台中为PostgreSQL显示表中的数据

Jen*_*sky 5 ruby sql postgresql activerecord ruby-on-rails

我找到这样的东西:Rails:如何使用Rails控制台列出数据库表/对象?

这条线很好:

ActiveRecord::Base.connection.tables
Run Code Online (Sandbox Code Playgroud)

并返回所有表

ActiveRecord::Base.connection.table_structure("users")
Run Code Online (Sandbox Code Playgroud)

生成错误:

ActiveRecord::Base.connection.table_structure("projects")
Run Code Online (Sandbox Code Playgroud)

我觉得

table_structure
Run Code Online (Sandbox Code Playgroud)

不是Postgres方法.

如何在Postgres数据库的Rails控制台中列出表中的所有数据?

hou*_*se9 11

您可以直接从命令行从postgres获取此信息

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
Run Code Online (Sandbox Code Playgroud)

如果要查看rails控制台中的Model属性

User.new
User.new.inspect

# or install the awesome_print gem for better output
ap User.new
Run Code Online (Sandbox Code Playgroud)