简体   繁体   English

如何从控制台获取所有表列作为Rails中的哈希?

[英]How to get all table columns as hash in rails from console?

The following code returns the table-names How do I get the individual column names within the table? 以下代码返回table-names 如何获得表中的各个列名?

ActiveRecord::Base.connection.tables
 #=> ["products","listings"]

How do I get: Products.< all columns > 我如何获得: Products.< all columns >

In Rails, the active record class of a table name is the singular form, and capitalized. 在Rails中,表名的活动记录类为单数形式并大写。

So to get the columns for the products table, you would use: 因此,要获取products表的列,可以使用:

Product.column_names

This provides an array of all the column names for the table represented by the ActiveRecord object, Product . 这提供了由ActiveRecord对象Product表示的表的所有列名称的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM