按寄存器显示所有授权

时间:2016-04-13 00:37:34

标签: sql ruby-on-rails ruby ruby-on-rails-4 rails-4-2-1

我有这个问题:

enter image description here

我有一个控制器:

  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>

1 个答案:

答案 0 :(得分:1)

在我查看数据库表之后,我有一个问题。看来一个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 %>