Created
February 23, 2010 05:54
-
-
Save marshluca/311914 to your computer and use it in GitHub Desktop.
动态定义class,连接database, 打印ddl
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 '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