lundi 25 avril 2016

Save my table historic with all the authorizations ids checked

I have a problem... I need save my table historic with all the authorizations ids checked, update the column authorization_origin_id and set the column refinancing_id with the refinancing id created. Example: I checked authorizations ids 2 and 3, so I create historic with two lines with authorization_origin_id 2 and 3 and the refinancing id will be 1. My code:

  def new
    if params[:authorization].present?
      @selected_ids = params[:authorization][:contract_number]
      @authorizations = Authorization.where("contract_number in (?)", @selected_ids)
      auth_params = params[:authorization]
      auth_params[:contract_number].zip(auth_params[:value_solve].reject(&:blank?)).each do |contract_number, value_solve|
          Authorization.where(contract_number: contract_number).update_all(value_solve: value_solve, situation: 2)
      end

      @authorizations.ids.each do |auth_id|
        @historic_refinancing = HistoricRefinancing.create
        @historic_refinancing = HistoricRefinancing.update_all(authorization_origin_id: auth_id)
      end

    end
    @employee = Employee.search_cpf(params[:search_employee_by_cpf])
    @refinancing = Refinancing.new

  end

My view:

<%= simple_form_for(@refinancing) do |f| %>
  <div class="form-inputs">
    <%= f.hidden_field :employee_id, value: @employee.first.id %>
    <%= f.hidden_field :authorization, value: @authorization  %>
    <%= f.input :contract_number %>
  </div>

  <h3>Reserved value</h3>
  <table class="table table-condensed table-bordered table-striped">
    <thead>
      <th>Authorization id</th>
      <th>Contract number</th>
    </thead>
    <% @authorizations.each do |authorization| %>
      <tbody>
        <tr>
          <td><%= authorization.id %></td>
          <td><%= authorization.contract_number %></td>
        </tr>
      </tbody>
    <% end %>
  </table>

  <div class="form-actions">
    <%= f.button :submit, "To Reserve" %>
  </div>
<% end %>

Actually my table historic is wrong, authorization_origin_id take, just the last (just one ever =/) value checked and refinancing_id is null




Aucun commentaire:

Enregistrer un commentaire