0

データベースの完全性のために単純な統計を取得しようとしています。入力されていないフィールドに「null」または「」があることを考慮して、レコードごとにフィールドの総数のうちいくつのフィールドが入力されているかを知りたいです。

既存のコードは次のようになります。

<% 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 %>
4

1 に答える 1

2

以下を試すことができます

#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)

参照column_names

于 2013-01-29T14:04:28.880 に答える