Skip to content

Instantly share code, notes, and snippets.

@dchentech
Created November 20, 2012 06:57
Show Gist options
  • Save dchentech/4116488 to your computer and use it in GitHub Desktop.
Save dchentech/4116488 to your computer and use it in GitHub Desktop.
在Rails外面使用ActiveRecord,并直接声明全部model
ActiveRecord::Base.establish_connection(YAML.load_file('./config/database.yml')['production'])
ActiveRecord::Base.connection.execute("show tables;").to_a.flatten.map do |table_name|
eval "class #{table_name.camelcase} < ActiveRecord::Base; self.table_name = :#{table_name}; end"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment