Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to group an object by its associated model when using a has_many :through relationship in rails?
    text
    copied!<p>I am working on an application that helps a local restaurant track the hours worked each day. The user enters data through a model called a "Checkout" at the end of each shift. They select an employee that is associated with a <code>has_many :through =&gt; employment</code>, where the employment model is a join table. Granted, <code>Checkout</code> is a confusing term, but that is the DSL that the restaurant wanted to use.</p> <p>How do you group the hours worked by each employee on a given day? There might be 2 checkouts in the database for that employee on that day – one from lunch and one from dinner.</p> <p>I want to create a list of hours that each employee worked on a given day when their hours from that day might be stored in separate checkouts in the database.</p> <pre><code>Today's Date Joe Employee Hours worked: 12 Jill Employee Hours worked: 4 etc. </code></pre> <p>How do I group the checkouts by employee when they are not an attribute on the checkouts model, but rather are an association through my employment model? Essentially, I'm trying to do something like this in my reports helper:</p> <pre><code>def checkouts_today current_user.checkouts.where( :date =&gt; Date.today ) end def employee_hours_today checkouts_today.where( :employment =&gt; Employee.find_by(id: params[:id]) ).sum(:hours) end </code></pre> <p>But I can't get it to work with the has_many :through association. I can only figure out how to total all hours from that day, not the total hours per employee.</p> <p>Here are the relevant files:</p> <ul> <li>models/checkout.rb - <a href="https://gist.github.com/leemcalilly/a2a0578adaf8841d4d5e" rel="nofollow">https://gist.github.com/leemcalilly/a2a0578adaf8841d4d5e</a></li> <li>models/employee.rb - <a href="https://gist.github.com/leemcalilly/2df5e7fb7db0ac0f602c" rel="nofollow">https://gist.github.com/leemcalilly/2df5e7fb7db0ac0f602c</a></li> <li><p>models/employment.rb - <a href="https://gist.github.com/leemcalilly/d3a028b6effe5f245b2a" rel="nofollow">https://gist.github.com/leemcalilly/d3a028b6effe5f245b2a</a></p></li> <li><p>helpers/reports_helper.rb - <a href="https://gist.github.com/leemcalilly/e2503188bb26df64ad20" rel="nofollow">https://gist.github.com/leemcalilly/e2503188bb26df64ad20</a></p></li> <li><p>view/payroll_processing.html.erb - <a href="https://gist.github.com/leemcalilly/868b1272128e75dc60d0" rel="nofollow">https://gist.github.com/leemcalilly/868b1272128e75dc60d0</a></p></li> <li><p>db/schema.rb - <a href="https://gist.github.com/leemcalilly/f9ce764d16161b7b3017" rel="nofollow">https://gist.github.com/leemcalilly/f9ce764d16161b7b3017</a></p></li> </ul> <p><strong>Update:</strong> I can loop through each employee and display their checkouts with this:</p> <pre><code>&lt;% @employees.each do |employee| %&gt; &lt;% if employee.checkouts.present? %&gt; &lt;p&gt;&lt;%= employee.full_name %&gt;&lt;/p&gt; &lt;% employee.checkouts.each do |checkout| %&gt; &lt;%= checkout.date %&gt; &lt;%= checkout.shift %&gt; &lt;%= checkout.hours %&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;% end %&gt; </code></pre> <p>But, I can't figure out how to sum their hours and display and return it in one block. It returns each checkout for each day under each employee's name. It also seems like this kind of logic should not be in the view.</p>
 

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