ruby-on-railsrubyruby-on-rails-4rails-4-2-1

Consult replicating values on digit cpf


I have a search that when digit a CPF show all results. Ok, but I need that show this way: (Just a person and the quantity of contracts that it has) * My code is in portuguese, sorry

Name
Registry
CPF
   Contract 1 ---- Show all contracts
   Contract 2   --------
   Contract 3 -----------

But it is currently coming so: (The same values according to the amount of contract)

Name
Registry
CPF
   Contract 1 ---- Show all contracts
   Contract 2   --------
   Contract 3 -----------

Name
Registry
CPF
   Contract 1 ---- Show all contracts
   Contract 2   --------
   Contract 3 -----------

Name
Registry
CPF
   Contract 1 ---- Show all contracts
   Contract 2   --------
   Contract 3 -----------

It is replicating values... because contract's number

My view is this:

<% if params[:pesquisa_func_cpf].present? %>
  <h4><b>Resultados</b></h4>
  <% @autorizacoes.each do |autorizacao| %>
    <table class="table table-condensed">
      <tr>
        <th>Name</th>
        <td><%= autorizacao.employee.person.name %></td>
      </tr>
      <tr>
        <th>Registry</th>
        <td><%= autorizacao.employee.registry %></td>
      </tr>
      <tr>
        <th>CPF</th>
        <td><%= autorizacao.employee.person.cpf %></td>
      </tr>
    </table>
    <hr />
    <table class="table table-condensed table-bordered">
      <th>Contract number</th>
      <% @autorizacoes.each do |autorizacao| %>
        <td><%= autorizacao.number_contract %></td>
      <% end %>
    </table>

  <% end %>
<% end%>

In this is my controller:

if params[:pesquisa_func_cpf].present? @autorizacoes = Autorizacao.pesquisa_func_cpf(params[:pesquisa_func_cpf]).all

I try the .distinct instead of .all, but don't work : (

And my consult (I use oracle), is it:

select * from autorizacoes INNER JOIN employers ON employers.id = autorizacoes.employer_id
                           INNER JOIN people ON employers.person_id = people.id
                           WHERE people.cpf  LIKE '111.111.111-11'

And it return 3 results, according my example. Please, how make just leave this structure:

Name
Registry
CPF
   Contract 1 ---- Show all contracts
   Contract 2   --------
   Contract 3 -----------

Solution

  • I got it! In my controller I just this:

    if params[:pesquisa_func_cpf].present?
          @employers = Employee.pesquisa_cpf(params[:pesquisa_func_cpf]).all
          @autorizacoes = Autorizacao.pesquisa_func_cpf(params[:pesquisa_func_cpf]).all
    

    And my view I remove <%= autorizacao.employee.person.name %> and put <%= employee.person.name %>

    Just this and works!