Skip to content

Instantly share code, notes, and snippets.

@justincampbell
Created March 20, 2012 20:23
Show Gist options
  • Save justincampbell/2140913 to your computer and use it in GitHub Desktop.
Save justincampbell/2140913 to your computer and use it in GitHub Desktop.
Table Stats
# table_stats User
# table_stats User, :email, :name
def table_stats(model, *fields)
old_logger = ActiveRecord::Base.logger
ActiveRecord::Base.logger = nil
fields = model.send :column_names if fields.empty?
model_count = model.send(:count)
puts "#{model_count} #{model.to_s.pluralize}"
fields.each do |field|
field_count = model.send(:where, "#{field} is not null").count
field_percent = ((field_count.to_f / model_count.to_f) * 100).round 2
puts "#{field} #{field_count} (#{field_percent}%)"
end
where = fields.map { |field| "#{field} is not null" }
not_null_count = model.send(:where, where.join(" or ")).count
not_null_percent = ((not_null_count.to_f / model_count.to_f) * 100).round 2
puts "(any) #{not_null_count} (#{not_null_percent})"
ActiveRecord::Base.logger = old_logger
nil
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment