<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -452,6 +452,7 @@ end
         (0..last_index).each{|i|result &lt;&lt; c[:proc].call(form,i)}
         return result
       else
+        
         return c[:proc].call(form,index)
       end
     end
@@ -821,7 +822,12 @@ end
         field_list.each do |f|
           the_field = form.fields[f]
           value = the_field.calculated[:proc].call(form,index)
-          idx = the_field.calculated[:summary_calculation] ? 0 : index
+          if the_field.calculated[:summary_calculation]
+            idx = 0
+            FieldInstance.destroy_all([condition_string,@form_instance.id,[f],0])
+          else
+            idx = index
+          end
           fi = FieldInstance.new({:answer =&gt; value, :field_id=&gt;f, :form_instance_id =&gt; @form_instance.id, :idx =&gt; idx, :state =&gt; 'calculated'})
           fi.save
         end</diff>
      <filename>lib/metaform/record.rb</filename>
    </modified>
    <modified>
      <diff>@@ -805,6 +805,7 @@ describe Record do
       @record['name',2] = 'Jane'
       @record['name',3] = 'Bob'
       @record.total_bobs.should == 2
+      @record['total_bobs',0].should == 2
       @record['total_bobs',4].should == 2
       @record.save('new_entry')
       r = Record.locate(@record.id,:fields =&gt; ['total_bobs'],:return_answers_hash =&gt;true)
@@ -818,6 +819,27 @@ describe Record do
       r = Record.locate(@record.id,:fields =&gt; ['reverse_name_and_job'],:return_answers_hash =&gt;true)
       r['reverse_name_and_job'].value.should == &quot;ssoBtrebreH&quot;
     end
+    it &quot;summary_calculation fields should be written to the database at the 0th index&quot; do
+      @record['name',1] = 'Joe'
+      @record['name',2] = 'Jane'
+      @record['name',3] = 'Bob'
+      @record.save('new_entry')
+      f = FieldInstance.find(:all,:conditions =&gt; ['form_instance_id = ? and field_id = &quot;total_bobs&quot;',@record.id])
+      f.size.should == 1
+      f[0].answer.should == &quot;2&quot; 
+    end
+    it &quot;summary_calculation fields should be written to the database at the 0th index, even when there is a value saved there already&quot; do
+      @record.save('new_entry')
+      record = Record.find(:first, :index =&gt; :any)
+      record['name',0].should == 'Bob Smith'
+      record['name',1] = 'Joe'
+      record['name',2] = 'Jane'
+      record['name',3] = 'Bob'
+      record.save('new_entry')
+      f = FieldInstance.find(:all,:conditions =&gt; ['form_instance_id = ? and field_id = &quot;total_bobs&quot;',@record.id])
+      f.size.should == 1
+      f[0].answer.should == &quot;2&quot; 
+    end    
   end
   
   describe &quot;-- explanations&quot; do</diff>
      <filename>spec/metaform/record_spec.rb</filename>
    </modified>
    <modified>
      <diff>@@ -45,7 +45,7 @@ def setup
     }    
     f 'total_bobs', :label =&gt; 'total number of Bobs', :type =&gt; 'integer', :calculated =&gt; {
       :based_on_fields =&gt; ['name'],
-      :summary_calculate =&gt; true,
+      :summary_calculation =&gt; true,
       :proc =&gt; Proc.new { |form,index| answers = form.get_record.answers_hash('name');answers['name'].count(&quot;answer =~ /Bob/&quot;)}
     }    
     f 'people_num', :label =&gt; '', :type =&gt; 'string'</diff>
      <filename>spec/resources/forms/sample_form.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>e8011e4c16be20c116853474b415ab903224b140</id>
    </parent>
  </parents>
  <author>
    <name>Lisa Sawin</name>
    <email>lsawin@optonline.net</email>
  </author>
  <url>http://github.com/zippy/metaform/commit/8f0599481b05ab391c3816c191fc5fdc91999d00</url>
  <id>8f0599481b05ab391c3816c191fc5fdc91999d00</id>
  <committed-date>2009-06-08T12:47:14-07:00</committed-date>
  <authored-date>2009-06-08T12:47:14-07:00</authored-date>
  <message>summary_calculations now properly save their values in the database

Before, if you updated the value of a field which was being used
to calculated a calculated field with summary_calculation set to
true, the database wouldn't save the value if there was already
one written for this field at the 0th index.
record#update_calculated_fields had to be updated to destroy
the 0th indexed value first, so it could be saved in this case.</message>
  <tree>60bac579b5154b5913b7a15b71a7c51cb6ad8d36</tree>
  <committer>
    <name>Lisa Sawin</name>
    <email>lsawin@optonline.net</email>
  </committer>
</commit>
