Skip to content

Instantly share code, notes, and snippets.

@denyago
Created March 2, 2013 10:49
Show Gist options
  • Save denyago/5070488 to your computer and use it in GitHub Desktop.
Save denyago/5070488 to your computer and use it in GitHub Desktop.
List all models and their DB columns
# Load models
Dir.foreach("app/models") { |f| require f if f =~ /rb$/ }
ActiveRecord::Base.descendants.each do |klass|
next if klass == "Rack::OAuth2::Server::Issuer".safe_constantize or klass == "Rack::OAuth2::Server::ActiveRecord".safe_constantize
begin
klass.columns.each_with_index {|c, i| printf "%-20s | %-19s | %-29s | %-10s\n", (i==0 ? klass.name : ''), c.name, c.sql_type, (c.null ? '' : 'not null') }
rescue => e
puts "*"*10
puts "Error getting columns for #{klass}: #{e}"
puts "*"*10
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment