Search code examples
sqlruby-on-railsrubyruby-on-rails-4rails-4-2-1

Show all authorizations by registers


I have this problem:

enter image description here

I have a controller:

  def index
    if params[:search_employee_by_cpf].present?
      @employees = Employee.search_cpf(params[:search_employee_by_cpf]).all
      @authorizations = Authorization.search_employee_by_cpf(params[:search_employee_by_cpf]).all
    end
  end

Model authorization:

  scope :search_employee_by_cpf, -> (cpf) { Authorization.joins("INNER JOIN employees ON employees.id = authorizations.employee_id INNER JOIN people ON employees.person_id = people.id").where("people.cpf LIKE ?", "%#{cpf}%") }

And model Employee:

  scope :search_cpf, -> (cpf) { joins(:person).where("people.cpf LIKE ?", "%#{cpf}%") }

I need two register/matricula and I need show authorizations for each register/matricula. Actually, show all authorization for each register and this is wrong! I just want show the authorizations of respective register. How make this?

UPDATE -------------------------

This is a part of my view responsable for show authorizations

<% @authorizations.each do |authorization| %>
            <tr>
              <td><%= check_box_tag 'authorization[contract_ids][]', authorization.contract_number %></td>
              <td><%= authorization.contract_number %></td>
              <td><%= number_to_currency authorization.parcel_value %></td>
              <td><%= authorization.qtd_parcel %></td>
              <td><%= number_to_currency authorization.total_value %></td>
              <td>
                <%= simple_form_for('/', remote: true) do |f| %>
                  <%= f.input :value_solve, label: false, placeholder: "Digit a value" %>
                  <%= f.submit 'Gravar valor' %>
                <% end %>
              </td>
            </tr>
          <% end %>

UPDATE 2 -------------------------

  def new
    if params[:authorization]
      @selected_ids = params[:authorization][:contract_ids]
      @authorizations = Authorization.where("contract_number in (?)", @selected_ids)
    end
    @employee = Employee.search_cpf(params[:search_employee_by_cpf])
    @refinancing = Refinancing.new
  end

new.html.erb (after checked and click on button go to new)

<table class="table table-condensed table-bordered">
  <tr>
    <td>Name:</td>
    <td><%= @employee.person.name %></td>
  </tr>
  <tr>
    <td>CPF:</td>
    <td><%= @employee.person.cpf %></td>
  </tr>
</table>

Solution

  • after I look at you database tables, I have a question. It appears that one employee can have only one person which contains cpf, in that case I don't think the scope method in Authorization is necessary anymore. Because obviously each employee's authorizations will all have only one cpf. you can simply just do

    @employees = Employee.search_cpf(params[:search_employee_by_cpf]).includes(:authorizations)
    

    in your view, change

    <% @authorizations.each do |authorization| %>
      <tr>
        <td><%= check_box_tag 'authorization[contract_ids][]', authorization.contract_number %></td>
        <td><%= authorization.contract_number %></td>
        <td><%= number_to_currency authorization.parcel_value %></td>
        <td><%= authorization.qtd_parcel %></td>
        <td><%= number_to_currency authorization.total_value %></td>
        <td>
          <%= simple_form_for('/', remote: true) do |f| %>
            <%= f.input :value_solve, label: false, placeholder: "Digit a value" %>
            <%= f.submit 'Gravar valor' %>
          <% end %>
        </td>
      </tr>
    <% end %>
    

    to

    <% employee.authorizations.each do |authorization| %>
      <tr>
        <td><%= check_box_tag 'authorization[contract_ids][]', authorization.contract_number %></td>
        <td><%= authorization.contract_number %></td>
        <td><%= number_to_currency authorization.parcel_value %></td>
        <td><%= authorization.qtd_parcel %></td>
        <td><%= number_to_currency authorization.total_value %></td>
        <td>
          <%= simple_form_for('/', remote: true) do |f| %>
            <%= f.input :value_solve, label: false, placeholder: "Digit a value" %>
            <%= f.submit 'Gravar valor' %>
          <% end %>
        </td>
      </tr>
    <% end %>