M
M
Max2015-12-04 22:31:01
Ruby on Rails
Max, 2015-12-04 22:31:01

How to make sum count of data from specific field in table (not DB) rails?

Hello, you need to calculate the sum of all "doctors' salaries from the table."
This is how I get the data

<% @doctor.each do |doc| %>
      <tr>
        <td><%= doc.doctor_first_name %></td>
        <td><%= doc.doctor_second_name %></td>
        <td><%= doc.current_money %></td>
        <td><%= link_to 'Переглянути', doc %></td>
      </tr>
    <% end %>

You need to add all the values ​​of the doc.current_money fields. The problem is that ActiveRecord will not help here, since these values ​​are not stored in the database, but are calculated in the model. Tell me how to get out of this situation.
That's where I get them from (Model doctor)
def current_money
    doctor_hour_price.to_f*users.where("enter_date >= ?", 7.days.ago).count
  end

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Mirilaczvili, 2015-12-05
@maxprof

I think the calculation of the sum of all the money earned by doctors in a week is done like this:

Doctor.
  joins(:users).
  group("users.id").
  where("enter_date >= ?", 7.days.ago).
  select("SUM(COUNT(users.id) * doctor_hour_price) as total_money").
  total_money

By the way, here 7 days means 7 × 24 hours ago, and not, say, from the beginning of the working day 7 days ago.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question