ruby on rails - How to get total only from items that have been saved to the database? -


in rails app have invoices can have many nested items.

class invoice < activerecord::base    attr_accessible :date, :number, :items_attributes    has_many :items    accepts_nested_attributes_for :items    def total     items.map(&:total).sum   end  end 

how can make sure total calculated on items have been saved database?

right now, total includes items have merely been instantiated in new view not yet saved database.

thanks help.

def total   items(true).map(&:total).sum end 

the true forces reload of items. or:

def total   items.select(&:persisted?).map(&:total).sum end 

persisted? true, if object in database (not new, not deleted).


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -