205

I was wondering if you could list/examine what databases/objects are available to you in the Rails console. I know you can see them using other tools, I am just curious. Thanks.

2
  • 2
    that's what script/dbconsole is for though Commented Jan 20, 2010 at 0:25
  • Yes, that drops you into mysql (or whatever). For some stranger reason I want to list columns/tables etc from regular console. I am thinking that it might require custom made ruby methods to do such a thing. Commented Jan 20, 2010 at 2:19

6 Answers 6

410

You are probably seeking:

ActiveRecord::Base.connection.tables

and

ActiveRecord::Base.connection.columns('projects').map(&:name)

You should probably wrap them in shorter syntax inside your .irbrc.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, the first one works like I want. But the second one doesn't - hunted for something similar but no luck.
ActiveRecord::Base.connection.columns("foos") should also work, but it returns column objects, .map{|c| [c.name, c.type] } on the end fixes that.
table_structure seems to be specific to the sqlite adapter.
For Rails4 use Model.column_names to list table columns
Also in Rails 4, you can just use ModelName to get a list of the columns and their data types.
|
34

I hope my late answer can be of some help.
This will go to rails database console.

rails db

pretty print your query output

.headers on
.mode columns
(turn headers on and show database data in column mode )

Show the tables

.table

'.help' to see help.
Or use SQL statements like 'Select * from cars'

2 Comments

'rails dbconsole' puts you in the command line interpreter (CLI) of whatever database engine you're using... the above are SQLite commands... Postgres, for example, would use '\dt' to list tables...
is there a way to ensure that every time you don't have to apply .headers on and .more columns when running rails db
9

To get a list of all model classes, you can use ActiveRecord::Base.subclasses e.g.

ActiveRecord::Base.subclasses.map { |cl| cl.name }
ActiveRecord::Base.subclasses.find { |cl| cl.name == "Foo" }

2 Comments

Also: ActiveRecord::Base.descendants.map(&:name)
In development you also need Rails.application.eager_load! before. Also it will not list all tables, for example it miss ActiveStorage tables.
6

You can use rails dbconsole to view the database that your rails application is using. It's alternative answer rails db. Both commands will direct you the command line interface and will allow you to use that database query syntax.

From the dbconsole command line:

  • \l Lists available databases
  • \d Lists tables available in the current database
  • \? (help) lists the many options for examining other sorts of objects

Comments

1

Run this:

Rails.application.eager_load! 

Then

ActiveRecord::Base.descendants

To return a list of models/tables

Comments

-3

Its a start, it can list:

models = Dir.new("#{RAILS_ROOT}/app/models").entries

Looking some more...

1 Comment

by doing this you just list models files what if tables are exists in db and do not have model fild in the models dir !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.