Skip to content

Instantly share code, notes, and snippets.

@marshluca
Created February 23, 2010 05:54
Show Gist options
  • Save marshluca/311914 to your computer and use it in GitHub Desktop.
Save marshluca/311914 to your computer and use it in GitHub Desktop.
动态定义class,连接database, 打印ddl
require 'rubygems'
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:database => "myapp",
:username => "root",
:password => "root",
:encoding => "utf8")
classes = %w(Place Activity)
classes.each do |name|
Object.const_set name, Class.new(ActiveRecord::Base)
end
def get_ddl(model)
model.columns_hash.each do |column|
puts "# #{column.first}---#{column.last.sql_type}---#{model.first.attributes[column.first]}"
end
end
get_ddl(Place)
get_ddl(Activity)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment