Created
November 14, 2012 18:04
-
-
Save fuzzmonkey/4073710 to your computer and use it in GitHub Desktop.
ActiveRecord extract database table information
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'active_record' | |
| ActiveRecord::Base.establish_connection( | |
| :host => "localhost", | |
| :user => "your_user", | |
| :adapter => "mysql2", | |
| :database => "your_database" | |
| ) | |
| ActiveRecord::Base.connection.tables.each do |table_name| | |
| model_class = class_eval "class #{table_name.classify} < ActiveRecord::Base; set_table_name '#{table_name}'; end" | |
| puts table_name | |
| model_class.columns.each do |col| | |
| puts "#{col.name}: #{col.sql_type}" | |
| end | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Quick and dirty way to extract columns / column type information from a MySQL database using ActiveRecord.