Reputation: 1796
I am trying to retrieve simple statistics for database completeness. I would like to know how many fields out of total number of fields have been filled in per record, considering that fields not entered have "null" in them or "".
Here is how the existing code looks like :
<% if @products %>
<% total_attribute_count = Product.columns.size %>
<% @products do |product| -%>
<p><%= platform.name %></p>
<p>I would like to have here a number of attributes filled in, so I could calculate a percentage based on total_attribute_count</p>
<% end -%>
<% end %>
Upvotes: 0
Views: 302
Reputation: 47482
You can try following
#Following line will gives you total number of columns in the table
a = Product.column_names.length
#Following line will gives you total number of columns which are filled
b = Product.column_names.map{|a| product.send(a)}.compact.length
#Following line gives you total number of columns which are nil or null
a - b
To calculate % you can do following
(b.to_f*100/a.to_f)
Ref column_names
Upvotes: 2