Note that there are some explanatory texts on larger screens.

plurals
  1. POSum of booking per user
    text
    copied!<p>I am trying to get the <code>sum</code> of the overtime per user <strong>NOT</strong> per booking which is what it is doing now: <a href="http://i.imgur.com/dhGgzGz.png" rel="nofollow">Imgur</a></p> <p><strong>Controller</strong></p> <pre><code>def index @users = User.all @hospital_bookings = HospitalBooking.scoped end end </code></pre> <p><strong>Index.html.erb</strong></p> <pre><code>&lt;%@users.each do |user|%&gt; &lt;% user.hospital_bookings.each do |hospital_booking| %&gt; { name:'&lt;%= hospital_booking.user.try(:name)%&gt;', data:[&lt;%= @hospital_bookings.where(:user_id =&gt; hospital_booking.user.id).all.sum(&amp;:overtime)%&gt;] }, &lt;% end %&gt; &lt;% end %&gt; </code></pre> <p><strong>Models</strong></p> <pre><code>#User.rb has_many :hospital_bookings #hospital_booking belongs_to :user </code></pre> <p><strong>Schema.rb</strong> </p> <pre><code>create_table "hospital_bookings", :force =&gt; true do |t| t.string "booking_reference" t.datetime "created_at", :null =&gt; false t.datetime "updated_at", :null =&gt; false t.integer "hospital_id" t.integer "user_id" t.date "day" t.string "shift_type" t.decimal "overtime", :precision =&gt; 10, :scale =&gt; 0 end </code></pre> <p>I have tried the following and these both do not seem to work. </p> <pre><code>**Attempt1** &lt;%= @hospital_bookings.where(:user_id =&gt; user.id, :hospital_booking =&gt; hospital_id ).all.sum(&amp;:overtime)%&gt; **Attempt2** &lt;%= HospitalBooking.where(user_id: user.id, hospital_id: hospital.id).sum(:overtime) %&gt; </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