0
  def self.build_prior_month(payee, as_of = Time.current, opts = {})    
    start_date, end_date, year, month = effective_dates(as_of)

    statement = payee.earnings_statements.build(
      :year => year,
      :month => month,
      :balance => 0,
      :revenue => 0
    )

    statement.generate(start_date, end_date, year, month)
    statement.update_totals

    statement
  end

这是我在我的模型中的一个功能。它statement正确返回,但不保存到数据库中。这是为什么?

generate函数如下所示:

  def generate_ledger_items(start_date, end_date, year, month)
    payee.ledger_entries.for_month_and_year(month, year).each do |entry|
      earnings_statement_items.build(
        :items => entry.item_group,
        :units => entry.units,
        :net_revenue => entry.net_revenue,
        :net_revenue_per_unit => [entry.net_revenue, entry.units].all? ? (entry.net_revenue / entry.units).round(2) : nil,
        :fees => entry.service_fees,
        :payments_collected => entry.spendings,
        :fee_terms => entry.terms || entry.description, # When would terms be set?
        :due => entry.credit || -entry.debit
      )
    end
  end

  def update_totals
    self.revenue  = earnings_statement_items.net_revenue
    self.balance = earnings_statement_items.total
  end
4

2 回答 2

1

您的build关联方法实际上并未将记录保存到数据库中。create将要。它类似于您的模型之间newcreate模型上的差异。

statement = payee.earnings_statements.create(
  :year => year,
  :month => month,
  :balance => 0,
  :revenue => 0
)

将保存您的记录。请参阅文档

于 2013-01-20T22:48:01.823 回答
1

.build 不保存对象

称呼:

statement.save
于 2013-01-21T00:01:02.127 回答