ActiveRecord: List columns in table from console

前端 未结 8 1338
遥遥无期
遥遥无期 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:05

    For a more compact format, and less typing just:

    Portfolio.column_types 
    
    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
提交回复
热议问题