Skip to content

Instantly share code, notes, and snippets.

@fuzzmonkey
Created November 14, 2012 18:04
Show Gist options
  • Select an option

  • Save fuzzmonkey/4073710 to your computer and use it in GitHub Desktop.

Select an option

Save fuzzmonkey/4073710 to your computer and use it in GitHub Desktop.
ActiveRecord extract database table information
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
@fuzzmonkey
Copy link
Author

Quick and dirty way to extract columns / column type information from a MySQL database using ActiveRecord.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment