Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy am I getting this error: undefined method `stats_by_day'
    primarykey
    data
    text
    <p>I'm building a Rails 4 app but am hitting this error. I'm sure it's something that is going to be very obvious to someone other than me. </p> <p>Here's my helper file which is throwing this error in line 10:</p> <pre><code>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 </code></pre> <p>The relevant model file is:</p> <pre><code>class Stat &lt; 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 </code></pre> <p>I'm working through the RailsCasts on charting but can't figure this part out. </p> <p>I am trying to create a graph that shows the combined attendance of church services at one campus. What am I missing here?</p> <p>So it seems that the line:</p> <pre><code> stats_by_day = Stat.total_grouped_by_day("january 1, 2013".to_date) </code></pre> <p>is returning nil.</p> <p>I have tried entering this command in the console:</p> <pre><code>Stat.select("date(date), sum(attendance) as total_attendance") </code></pre> <p>And it also returns nil</p> <pre><code>Stat.first </code></pre> <p>returns</p> <pre><code> Stat Load (0.4ms) SELECT "stats".* FROM "stats" ORDER BY "stats"."id" ASC LIMIT 1 =&gt; #&lt;Stat id: 1, attendance: 41, salvations: 0, visitors: 7, offering: #&lt;BigDecimal:7fb85b20b618,'0.2187E4',9(36)&gt;, 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"&gt; </code></pre> <p>So it looks like the issue is somewhere in how I am calling the select method. Any ideas?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload