我正在尝试通过电子邮件发送月度报告,该报告与过去几个月相比很简单。
我在我的报表控制器中完成了所有这些工作(它引用了另一个名为 Clinical 的模型):
# Clinical income by month report
@clinical_income_by_month = Clinical.select(%q{date_format(transactiondate, '%Y') as year, date_format(transactiondate, '%M') as month, sum(LineBalance) as income})
.where(:payments => 0)
.where('linebalance <> ?', 0)
.where('analysiscode <> ?', 213)
.group(:monthyear)
然后我有一个包含数据的表的部分:
<div class="row">
<div class="twelve columns"><h5>This months income so far is <span style="font-size:1.2em"class="<% if @clinical_income_by_month.first.income >= @clinical_income_by_month.first(:offset => 12).income %>green<% else %>red<% end %> radius label"><%= number_to_currency(@clinical_income_by_month.first.income, :unit => "£", :separator => ".", :delimiter => ",") %></span></h5> <%= @clinical_income_by_month.first.month %> <%= @clinical_income_by_month.first(:offset => 12).year %>'s income was <%= number_to_currency(@clinical_income_by_month.first(:offset => 12).income, :unit => "£", :separator => ".", :delimiter => ",") %>.</div>
</div>
<hr />
<table>
<tr>
<th>Year</th>
<th>Month</th>
<th>Income</th>
</tr>
<% @clinical_income_by_month.each do |c| %>
<tr>
<td><%= c.year %></td>
<td><%= c.month %></td>
<td><%= number_to_currency(c.income, :unit => "£", :separator => ".", :delimiter => ",") %></td>
</tr>
<% end %>
</table>
我希望能够将那部分收入提取到我的电子邮件中,或者如果这不可能,我想显示最后收入的价值。
<%= @clinical_income_by_month.first.income %>
我的电子邮件代码如下所示:
Finance Report
================================================
<%= number_to_currency(@clinical_income_by_month.first.income) %>
我得到的错误是:
1.9.2-p318 :009 > UserMailer.finance_report().deliver
ActionView::Template::Error: undefined method `first' for nil:NilClass
from /Users/dannymcclelland/Projects/premvet/app/views/user_mailer/finance_report.text.erb:3:in `_app_views_user_mailer_finance_report_text_erb___4501411113534248604_70358523362460'
当我从标准方法中提取一个值时,它会起作用:
<%= number_to_currency(Clinical.first.UserID) %>
但不是当我从我创建的@clinical_income_by_month 报告中调用它时。
任何指针将不胜感激!