【问题标题】:Show data in table from Rails Console for PostgreSQL从 Rails Console for PostgreSQL 的表中显示数据
【发布时间】:2014-11-08 05:13:11
【问题描述】:

我发现这样的东西:Rails: How to list database tables/objects using the Rails console?

这条线很好:

ActiveRecord::Base.connection.tables

并返回所有表

但是

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

产生错误:

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

我认为

table_structure

不是 Postgres 方法。

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

【问题讨论】:

    标签: sql ruby-on-rails ruby postgresql activerecord


    【解决方案1】:

    您可以直接从命令行从 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
    

    如果你想在 Rails 控制台中查看模型属性

    User.new
    User.new.inspect
    
    # or install the awesome_print gem for better output
    ap User.new
    

    【讨论】:

    • 不要忘记:\q 退出 psql 控制台。刚刚为您保存了搜索;)
    • @grooble - 以上使用-c 选项,因此它执行命令但不进入交互式psql 会话,在这种情况下不需要\q
    猜你喜欢
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 2010-10-20
    • 1970-01-01
    • 1970-01-01
    • 2017-02-07
    • 2016-07-24
    • 2015-03-11
    相关资源
    最近更新 更多