Yu-Chieh’s Blog (Y.C. Chang)

Ruby on Rails / Rubygems / FullStack / Git / Mac notes.

Rails Console Tips: Table Operations

In rails console, you can

list tables

1
> ActiveRecord::Base.connection.tables

list a table structure (but it is a protected method)

1
2
3
4
5
6
# this dose not work. 
> ActiveRecord::Base.connection.table_structure("projects")

# we should utilize send method to call protected method
> @c = ActiveRecord::Base.connection
> @c.send(:table_structure, "xxxs") # xxxs is your model name

show table schema

1
> ActiveRecord::Base.connection.columns :table_name

Comments