Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby on Rails: hours worked this week
    text
    copied!<p>I have a "Log" project, each log has a :date, :hours, :description. I am simply trying to determine how many hours I have worked in a week, but am having trouble determining the proper separation of code. Let me know if any further code is needed. Rails 3.</p> <p><strong>log.rb</strong></p> <pre><code>def self.days_in_range(from, to) Log.where(:date =&gt; (from.to_date)..(to.to_date)) end </code></pre> <p><strong>index.html.erb</strong></p> <pre><code>&lt;% content_for :sidebar do %&gt; &lt;h4&gt; Sidebar Content &lt;/h4&gt; &lt;ul&gt; &lt;li&gt;Hours worked this week: &lt;%= Log.hours_this_week %&gt; # unsure how to call &lt;/li&gt; &lt;li&gt;Hours worked in total: &lt;%= Log.sum(:hours) %&gt; &lt;/li&gt; &lt;li&gt;Most hours worked in a day: &lt;%= Log.maximum(:hours) %&gt; &lt;/li&gt; &lt;/ul&gt; &lt;% end %&gt; </code></pre> <p><strong>logs_helper.rb?</strong></p> <pre><code>def hours_this_week today = Time.now day_of_week = today.wday sunday = today - day_of_week.days days = Log.days_in_range(today, sunday) hours = 0 days.each do |day| hours += day.hours end end </code></pre> <p>[solved] <strong>error</strong></p> <pre><code>Showing /Users/***/Documents/workspace/***/hours_tracker/hours/app/views/logs/index.html.erb where line #33 raised: undefined method `hours_this_week' for #&lt;LogsController:0x103b66be8&gt; Extracted source (around line #33): 30: &lt;h4&gt; Sidebar Content &lt;/h4&gt; 31: &lt;ul&gt; 32: &lt;li&gt;Hours worked this week: 33: &lt;%= hours_this_week %&gt; 34: &lt;/li&gt; 35: &lt;li&gt;Hours worked in total: 36: &lt;%= Log.sum(:hours) %&gt; Rails.root: /Users/***/Documents/workspace/***/hours_tracker/hours </code></pre> <p><a href="http://pastebin.com/NxX3Vfa0" rel="nofollow">full trace</a></p> <p>[updated] <strong>new error</strong></p> <p><strong>error</strong></p> <pre><code>ArgumentError in Logs#index Showing /Users/***/Documents/workspace/***/hours_tracker/hours/app/views/logs/index.html.erb where line #33 raised: wrong number of arguments (0 for 1) Extracted source (around line #33): 30: &lt;h4&gt; Sidebar Content &lt;/h4&gt; 31: &lt;ul&gt; 32: &lt;li&gt;Hours worked this week: 33: &lt;%= hours_this_week %&gt; 34: &lt;/li&gt; 35: &lt;li&gt;Hours worked in total: 36: &lt;%= Log.sum(:hours) %&gt; Rails.root: /Users/***/Documents/workspace/***/hours_tracker/hours </code></pre>
 

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