我正在构建一个 Rails 4 应用程序,但遇到了这个错误。我敢肯定,这对于我以外的其他人来说是非常明显的。
这是我的帮助文件,它在第 10 行抛出了这个错误:
module StatsHelper
def stats_chart_data
@start = "january 1, 2013".to_date
@start = @start - (@start.wday).days
stats_by_day = Stat.total_grouped_by_day("january 1, 2013".to_date)
(@start..Date.today).step(7).map do |date|
{
date: date,
attendance: stats_by_day(date).first.try(:total_attendance) || 0 #error shows up here
}
end
end
end
相关模型文件为:
class Stat < ActiveRecord::Base
belongs_to :campus
validates :attendance, presence: {message: "cannot be blank"}
validates :campus_id, presence: {message: "Please choose which campus"}
validates :date, presence: {message: "Please enter date"}
validates :time, presence: {message: "Please enter time"}
def self.total_grouped_by_day(start)
stats = where(date: start.beginning_of_day..Time.zone.now)
stats = stats.group("date")
stats = stats.select("date, sum(attendance) as total_attendance")
stats.group_by { |o| o.date.to_date }
end
end
我正在通过 RailsCasts 绘制图表,但无法弄清楚这部分。
我正在尝试创建一个图表,显示一个校园中教堂服务的总出席率。我在这里想念什么?
所以似乎这条线:
stats_by_day = Stat.total_grouped_by_day("january 1, 2013".to_date)
返回零。
我尝试在控制台中输入此命令:
Stat.select("date(date), sum(attendance) as total_attendance")
它也返回 nil
Stat.first
返回
Stat Load (0.4ms) SELECT "stats".* FROM "stats" ORDER BY "stats"."id" ASC LIMIT 1
=> #<Stat id: 1, attendance: 41, salvations: 0, visitors: 7, offering: #<BigDecimal:7fb85b20b618,'0.2187E4',9(36)>, created_at: "2013-07-17 05:33:28", updated_at: "2013-07-18 19:29:47", campus_id: 1, date: "2013-07-14", time: "2000-01-01 19:00:00">
所以看起来问题出在我如何调用 select 方法的某个地方。有任何想法吗?