title: How to get total only from items that have been saved to the database?
body: <p>In my Rails app I have <code>invoices</code> which can have many nested <code>items</code>.</p>
  
  <pre><code>class Invoice &lt; ActiveRecord::Base
  
    attr_accessible :date, :number, :items_attributes
  
    has_many :items
  
    accepts_nested_attributes_for :items
  
    def total
      items.map(&amp;:total).sum
    end
  
  end
  </code></pre>
  
  <p>How can I make sure the <code>total</code> is only calculated on <code>items</code> that have actually been saved to the database?</p>
  
  <p>Right now, my <code>total</code> also includes <code>items</code> which have merely been instantiated in my <code>new</code> view but not yet saved to the database.</p>
  
  <p>Thanks for your help.</p>