sql – 按寄存器显示所有授权

我有这个问题:

《sql – 按寄存器显示所有授权》

我有一个控制器:

  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

型号授权:

  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}%") }

和模型员工:

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

我需要两个寄存器/ matricula,我需要为每个寄存器/ matricula显示授权.实际上,显示每个注册的所有授权,这是错误的!我只想展示各自注册的授权.怎么做到这个?

更新————————-

这是我对视图授权负责的视图的一部分

<% @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 %>

更新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(选中后点击按钮转到新)

<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>

最佳答案 在我看了你的数据库表之后,我有一个问题.似乎一个员工只能拥有一个包含cpf的人,在这种情况下,我不认为授权中的范围方法是必要的.因为显然每个员工的授权都只有一个cpf.你可以简单地做

@employees = Employee.search_cpf(params[:search_employee_by_cpf]).includes(: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 %>

<% 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 %>
点赞