class Mood < ActiveRecord::Base default_scope { order(:recorded_at) } validates_presence_of :mode validates_presence_of :recorded_at def self.log return if Mood.count < 1 first_mood = Mood.first.recorded_at.to_date first_monday = first_mood - (first_mood.wday - 1) current_date = first_monday current_mode = nil log_mood = [] Mood.all.each do |mood| while current_date < mood.recorded_at.to_date do log_mood << [ current_date.to_s, current_mode ] current_date += 1 end current_mode = mood.mode end while current_date <= Date.today do log_mood << [ current_date.to_s, current_mode ] current_date += 1 end log_mood.each_slice(7).to_a.last(52) end end