Friday, August 12, 2016

How to list the tables and the table structure in Rails console?

In Rails 5:

The tables method has been deprecated:

DEPRECATION WARNING: #tables currently returns both tables and views. This behavior is deprecated and will be changed with Rails 5.1 to only return tables. Use #data_sources instead. (called from irb_binding at (irb):2)

So, you can list all the tables like this:

> ActiveRecord::Base.connection.data_sources
=> ["schema_migrations", "ar_internal_metadata", "products"]

You can then list the columns and it's type like this:{ |c| [, c.type]}
[["id", :integer], 
["name", :string], 
["price", :decimal], 
["created_at", :datetime], 
["updated_at", :datetime]]

No comments:

Post a Comment