Skip to content

Instantly share code, notes, and snippets.

@Catharz
Created June 11, 2014 04:05
Show Gist options
  • Save Catharz/fe237dc5e1d19936627b to your computer and use it in GitHub Desktop.
Save Catharz/fe237dc5e1d19936627b to your computer and use it in GitHub Desktop.
Create Rails models for every table in the database
invalid_chars = [' ', '$', '(', ')']
ActiveRecord::Base.connection.tables.each do |table_name|
name = table_name
invalid_chars.map { |ch| name = name.gsub(ch, '_') }
name = name.camelize
name = name + "_" if Module.const_defined? name
eval %{
class #{name} < ActiveRecord::Base
self.table_name = "#{table_name}" ;
self.primary_key = "#{ActiveRecord::Base.connection.primary_key table_name}" ;
end
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment