NoMethodError:nil:NilClass的未定义方法“ created_at”

时间:2019-01-04 13:26:51

标签: ruby-on-rails activerecord rails-activerecord

所以我正在围绕Rails模型的作用域进行研究。

这些查询在开发环境中完美地起作用,但在生产环境中却没有。

这是我的Fee模型,从那里我触发active-record查询以从StudentFeeInvoice模型中获取特定发票。

app / models / fee.rb

most_recent_invoice = aStudent.student_fee_invoices.invoices_except_pending_and_adjusted.order(:created_at).last
new_filter_for_invoices = aStudent.student_fee_invoices.invoices_after_specific_date(most_recent_invoice.created_at)"

app / model / student_fee_invoice.rb

scope :invoices_except_pending_and_adjusted, -> {where("status != ? AND status != ?", "Pending", "Adjusted")}
scope :invoices_after_specific_date, -> (created_date) {where('created_at > ?', created_date)}

请纠正我我在做什么错。 预先感谢!

1 个答案:

答案 0 :(得分:1)

最可能是aStudent.student_fee_invoices.invoices_except_pending_and_adjusted.order(:created_at).last 正在返回nil

在这种情况下,您将需要以下内容:

new_filter_for_invoices = aStudent.student_fee_invoices.invoices_after_specific_date(most_recent_invoice ? most_recent_invoce.created_at : DateTime.now)