title: Sum of attributes values in model. Rails 3
body: <p>I'm working on a model that has many attributes, some are the sum of others. I would like to know if this is an efficient way to do it and if I should be aware of performance issues. The model is about 200 decimal fields and 30 fields summing the others...</p>
  
  <p>This is what I have:</p>
  
  <pre><code>Class  FinancialDatum &lt; ActiveRecord::Base 
    #---rest omitted
  
    before_save :totalize
  
    def totalize
      totalizable = [:total_1,:total2] #etc..
      totalizable.each do |t|
        unless totalizable_for(t).map{|m|self.send(m).present?}.include? false
          self[t] = (totalizable_for(t).map{|num| self.send(num)}).inject(0, :+)
       end
     end
  
      def totalizable_for(att)
      case att
        when :total_1
          [:debt_1, :debt_2, :deb_t_3]
        when :total_2
          [debt_4, debt_5]
        end
      end
  
  end
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>Input and output is quite simple, user fills in <code>:debt_1 =&gt; 2, :debt_2 =&gt; 3, :debt_3 =&gt; 4</code> and the model should save <code>:total_1 =&gt; 9</code></p>
  
  <p>Thanks.</p>