在我的 Rails 应用程序中invoices
,我有很多嵌套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
如何确保total
仅计算items
实际已保存到数据库的值?
现在,我的total
还包括items
仅在我的new
视图中实例化但尚未保存到数据库中的内容。
谢谢你的帮助。