ActiveRecord: List columns in table from console

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

    complementing this useful information, for example using rails console o rails dbconsole:

    Student is my Model, using rails console:

    $ rails console
    > Student.column_names
     => ["id", "name", "surname", "created_at", "updated_at"] 
    
    > Student
     => Student(id: integer, name: string, surname: string, created_at: datetime, updated_at: datetime)
    

    Other option using SQLite through Rails:

    $ rails dbconsole
    
    sqlite> .help
    
    sqlite> .table
    ar_internal_metadata  relatives             schools             
    relationships         schema_migrations     students 
    
    sqlite> .schema students
    CREATE TABLE "students" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar, "surname" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
    

    Finally for more information.

    sqlite> .help
    

    Hope this helps!

提交回复
热议问题