ActiveRecord: List columns in table from console

前端 未结 8 1339
遥遥无期
遥遥无期 2020-12-22 23:21

I know that you can ask ActiveRecord to list tables in console using:

ActiveRecord::Base.connection.tables

Is there a command that would li

8条回答
  •  离开以前
    2020-12-23 00:08

    • To list the columns in a table I usually go with this:
      Model.column_names.sort.
      i.e. Orders.column_names.sort

      Sorting the column names makes it easy to find what you are looking for.

    • For more information on each of the columns use this:
      Model.columns.map{|column| [column.name, column.sql_type]}.to_h.

    This will provide a nice hash. for example:

    {
       id => int(4),
       created_at => datetime
    }
    

提交回复
热议问题