4

我有这个问题:

在此处输入图像描述

我有一个控制器:

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

我需要两个注册表/矩阵,并且我需要为每个注册表/矩阵显示授权。实际上,显示每个寄存器的所有授权,这是错误的!我只想显示各个寄存器的授权。这个怎么做?

更新 - - - - - - - - - - - - -

这是我对演出授权负责的部分观点

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

1 回答 1

1

在我看了你的数据库表之后,我有一个问题。似乎一个人employee只能有一个personcontains cpf,在这种情况下,我认为Authorization不再需要 scope 方法。因为显然每个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 %>
于 2016-04-13T03:11:09.003 回答