M
M
Max2015-12-04 14:17:48
Ruby on Rails
Max, 2015-12-04 14:17:48

How to display the value of a controller variable for each user (rails)?

Hello, I have a database of doctors. There is a field for the cost of an hour of a doctor's work. In the controller, I count how many patients he has, multiply by the cost of the work and find out how much the doctor received money.
There are many doctors themselves, each has its own cost and number of patients. I want to display a table of all doctors in the hospital with a column, let's say "salary". Where it will be indicated how much each of them received money (then I will make a schedule).
Here is the controller code. The code works, but I think it can be made simpler

class DoctorsController < ApplicationController
  before_action :set_doctor, only: [:show, :edit, :update, :destroy]
  def index
    @doctor = Doctor.all
  end

  def show
    @length =  User.where(doctor_id: params[:id]).size

    @length_length_for_seven_days = User.where("enter_date >= ?", 7.days.ago)
    @current_doctor_patients = @length_length_for_seven_days.where(doctor_id: params[:id]).size
    @hour_price = current_doctor.doctor_hour_price
    @current_money = @current_doctor_patients * @hour_price
  end

  private 

   def set_doctor
        @doctor = Doctor.find(params[:id])
     end
    
end

Doctor table output code:
<tbody>
  <tbody>
    <% @doctor.each do |doc| %>
      <tr>
        <td><%= doc.doctor_first_name %></td>
        <td><%= doc.doctor_second_name %></td>
        <td><%= @current_money %></td>
        <td><%= link_to 'Переглянути', doc %></td>
        <td><%= link_to 'Редагувати', edit_doctor_path(doc) %></td>
        <td><%= link_to 'Видалити', doc, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>

But instead of @current_money - empty strings. And if I write doc.current_money, it gives an error. Perhaps you need to save the value of this variable in the database and only then display it. Tell me how to do it please.
Update
Doctor model code. Correction. My patients are subscribed users
class Doctor < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  has_many :users
  has_many :prices
  has_many :news
  has_many :appointments

  def current_money
    doctor_hour_price*users.where("enter_date >= ?": 7.days.ago).count
  end
end

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dima, 2015-12-04
@maxprof

In the `Doctor` model, declare a `current_money` method that will calculate the current salary.
It will be something like this:

class Doctor < ActiveRecord::Base

  has_many :patients, class_name: 'User'

  def current_money
    doctor_hour_price.to_f * patients.where("enter_date >= ?", 7.days.ago).count
  end
end

<tbody>
  <tbody>
    <% @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>
        <td><%= link_to 'Редагувати', edit_doctor_path(doc) %></td>
        <td><%= link_to 'Видалити', doc, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question