Ruby on rails Rails-在保存之前迭代的嵌套属性有很多关联

Ruby on rails Rails-在保存之前迭代的嵌套属性有很多关联,ruby-on-rails,Ruby On Rails,我有一个模特,和很多人有联系 说 您可以使用和作为: @发票。分配属性参数[:发票] 总计=@invoice.line.选择&:标记_进行销毁?.map&:amount.sum @invoice.save全部保存在数据库上 这是可能的,但我需要见控制员提出具体建议。我已经更新了问题,并说明了我目前正在做的事情。 class Invoice < ActiveRecord::Base has_many :lines, class_name: 'InvoiceLine', inverse_o

我有一个模特,和很多人有联系

您可以使用和作为:

@发票。分配属性参数[:发票] 总计=@invoice.line.选择&:标记_进行销毁?.map&:amount.sum @invoice.save全部保存在数据库上


这是可能的,但我需要见控制员提出具体建议。我已经更新了问题,并说明了我目前正在做的事情。
class Invoice < ActiveRecord::Base
  has_many :lines, class_name: 'InvoiceLine', inverse_of: :invoice, dependent: :destroy
  accepts_nested_attributes_for :lines, reject_if: :invalid_line, allow_destroy: true
end
total = params["invoice"]["lines_attributes"].
          map{|k,v| [v["amount"], v["_destroy"]]}.
          select{|x| x[1] == "false"}.
          map{|x| x[0].to_f}.
          inject {|total, amount| total + amount}