【问题标题】:Table historic with all the authorizations ids checked update column具有所有授权 ID 检查更新列的历史表
【发布时间】:2016-04-26 12:42:35
【问题描述】:

我有这个问题...我需要保存我的历史表并检查所有授权 ID,更新列 authorization_origin_id 并使用创建的再融资 ID 设置列 refinancing_id。示例:我检查了授权 id 2 和 3,因此我创建了具有授权_origin_id 2 和 3 的两行的历史记录,再融资 id 将为 1。我的代码:

我的看法是这样的:

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

控制器是这样的:

 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

实际上我的表历史记录是错误的,authorization_origin_id 取值,只是检查了最后一个(只有一个 =/)值,refinancing_id 为空

【问题讨论】:

标签: sql ruby-on-rails ruby ruby-on-rails-4 sql-update


【解决方案1】:

答案是:

@historic_refinancing = HistoricRefinancing.where(authorization_origin_id: auth_id).update_all(authorization_origin_id: auth_id)

或者只是这个

@historic_refinancing = HistoricRefinancing.create(authorization_origin_id: auth_id)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-02
    • 1970-01-01
    • 2022-08-14
    • 2011-02-15
    • 2016-03-05
    • 2021-10-21
    相关资源
    最近更新 更多