1
我正在構建一個Rails 4應用程序,但遇到了這個錯誤。我相信這對於除我之外的其他人來說是非常明顯的。爲什麼我得到這個錯誤:undefined method`stats_by_day'
這裏是將其在管線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)
是返回nil。
我試圖進入控制檯命令:
Stat.select("date(date), sum(attendance) as total_attendance")
而且它也返回零
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方法。有任何想法嗎?
改變括號方後(如果你指定哪個行你得到的誤差會更容易)括號我得到以下錯誤:未定義的方法'第一'爲零:NilClass 錯誤與我的幫手 –
中的第10行有關,這意味着'Stat.total_grouped_by_day(「2013年1月1日」.to_date)'ret無零。 –
我看到在控制檯中,但我無法弄清楚爲什麼。 Stat.select(「date(date),sum(attendance)as total_attendance」) 在控制檯返回零,但我找不出原因。表中有與日期和出席記錄。 –