按寄存器显示所有授权

Show all authorizations by registers

我有这个问题:

我有一个控制器:

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

我需要两个 register/matricula 并且每个 register/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>

看了你的数据库表后,我有一个问题。看来一个 employee 只能有一个 person 其中包含 cpf,在这种情况下,我认为 Authorization 中的范围方法不再是必要的。因为显然每个employee的权限都只有一个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 %>