ActiveRecord: List columns in table from console
Solution 1
This will list the column_names from a table
Model.column_names
e.g. User.column_names
Solution 2
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
Solution 3
Using rails three you can just type the model name:
> User
gives:
User(id: integer, name: string, email: string, etc...)
In rails four, you need to establish a connection first:
irb(main):001:0> User
=> User (call 'User.connection' to establish a connection)
irb(main):002:0> User.connection; nil #call nil to stop repl spitting out the connection object (long)
=> nil
irb(main):003:0> User
User(id: integer, name: string, email: string, etc...)
Solution 4
If you are comfortable with SQL commands, you can enter your app's folder and run rails db
, which is a brief form of rails dbconsole
. It will enter the shell of your database, whether it is sqlite or mysql.
Then, you can query the table columns using sql command like:
pragma table_info(your_table);
Solution 5
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!
Comments
-
Andrew over 2 years
I know that you can ask ActiveRecord to list tables in console using:
ActiveRecord::Base.connection.tables
Is there a command that would list the columns in a given table?
-
owl about 13 yearsOP just wants the column names.
-
Yule about 13 yearsPerhaps. But not necessarily. It is an alternative way of getting them with extra info that is sometimes helpful when listing columns from console
-
Andrew about 13 yearsThis is also a useful method to know, IMO. @Yule - does this query the schema/migrations code etc. or does it query the DB? The reason I ask is I was experiencing a mismatch between my schema and what actually was in the DB (one migration glitched), so specifically I needed to be sure I was seeing what was actually in the table.
-
srt32 about 10 yearsYou could also run something like
Model.columns
to get more info about the columns including database config data. -
sayap about 10 yearsIn rails 3.2, doing it this way somehow doesn't set the
primary
attribute correctly (all columns haveprimary=nil
). It is set correctly with theModel.columns
method suggested by srt32. -
Yule over 9 years@Andrew it queries the DB (hence the need to establish a connection in rails 4)
-
nibbex about 9 yearsGreat! Using
Model.columns
provides all the information for a table through ActiveRecord. Crucially for me it was the only and easiest way to gain confidence in what my primary key really was at the database level. -
AJFaraday over 8 yearsYou could always use Model.primary_key, which gives you the name of the primary key according to rails. (This will be 'id' unless it's declared in the model as something else).
-
valk about 7 yearsFor mySQL use
describe your_table;
, not perfect but works -
oklas about 7 yearsYou can use online database console (gem activeadmin-sqlpage) as describerd in this answer if you use active admin.
-
Pak over 4 yearsDoes not exist in rails 5+
-
baash05 about 4 yearsThis is the right answer. There is no requirement to have a model. Not every table has a model. "has_many_and_belongs_to"
-
Richard_G about 3 yearsTo provide a list sorted by table name, use: ActiveRecord::Base.connection.tables.sort.each
-
A moskal escaping from Russia about 3 yearsThe answer doesn't state that, and the question is tagged Rails 3 @Pak...
-
Pak about 3 years@SebastianPalma I know, but as this is a well-referenced question such comments are imo helpful for people looking for answers, fast. The tag was probably chosen during the initial write-up but the question is quite generic.