4

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>
Elton Santos
  • 571
  • 6
  • 32

1 Answers1

1

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 %>
Zhiliang Xing
  • 1,057
  • 1
  • 8
  • 19
  • Get a error, I make equal you did... Association named 'authorizations' was not found on Employee; perhaps you misspelled it? – Elton Santos Apr 13 '16 at 03:46
  • 1
    in your `Employee` model, add `has_many :authorizations`, you have `belongs_to :employee` in you `Authorization` model. I think you missed it – Zhiliang Xing Apr 13 '16 at 03:52
  • Yes, employee can have only one person which contains cpf. Authorization belongs to employee and employee has many refinancings... A employee has many register (but register is only a column) – Elton Santos Apr 13 '16 at 03:52
  • Dammm, when I click on button bronken next view ... undefined method `person' for # – Elton Santos Apr 13 '16 at 03:59
  • how come, I think you have `belongs_to :person` in `Employee` model – Zhiliang Xing Apr 13 '16 at 04:01
  • wait, it is ``? then it is wrong, this is a collection like an array, you cannot call `person` on it – Zhiliang Xing Apr 13 '16 at 04:10
  • the solution that i think is put @employee.first.person.name... but it too is wrong – Elton Santos Apr 13 '16 at 04:12
  • Hey <%= @employee.first.person.name %> solve my problem, but I can do it? – Elton Santos Apr 13 '16 at 04:18
  • The method is new and is here: https://github.com/eltonsantos/playing_checkboxes/blob/master/app/controllers/refinancings_controller.rb - @employee is the same employee of before view. – Elton Santos Apr 13 '16 at 04:24
  • 1
    it is not ok unfortunately , coz from your graph, cpf is not unique in `Person` Model. – Zhiliang Xing Apr 13 '16 at 04:28
  • 1
    try to describe your logic in `new` action, which employee you want to query? if you only search by using cpf, in the case you show in the first graph, you will find two employees, not one – Zhiliang Xing Apr 13 '16 at 04:29
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108990/discussion-between-zhiliang-takuto-xing-and-elton-santos). – Zhiliang Xing Apr 13 '16 at 04:31