ActiveRecord: List columns in table from console

前端 未结 8 1340
遥遥无期
遥遥无期 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-22 23:44

    This gets the columns, not just the column names and uses ActiveRecord::Base::Connection, so no models are necessary. Handy for quickly outputting the structure of a db.

    ActiveRecord::Base.connection.tables.each do |table_name|
      puts table_name
      ActiveRecord::Base.connection.columns(table_name).each do |c| 
        puts "- #{c.name}: #{c.type} #{c.limit}"
      end
    end
    

    Sample output: http://screencast.com/t/EsNlvJEqM

提交回复
热议问题