使用rails

时间:2016-04-26 20:05:28

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

当我创建历史时,我需要这样做:

Table historic
id authorization_origin_id
1   2
2   3

你如何在图像中看到

enter image description here

这是我的new.html.erb

<%= simple_form_for(@refinancing) do |f| %>
  <div class="form-inputs">
    <%= f.hidden_field :employee_id, value: @employee.first.id %>
    <%= 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.where(authorization_origin_id: auth_id).update_all(authorization_origin_id: auth_id)
      end

    end

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

  end

目前,出现此错误:

enter image description here

authorization_origin_id为null,请问我是怎么做到的?

1 个答案:

答案 0 :(得分:1)

在您的控制器中,您创建的HistoricRefinancing没有任何参数。

HistoricRefinancing.create使用null authorization_origin_id,refinancing_id和authorization_new_id创建一个条目。

以下代码不执行任何操作,它会尝试使用HistoricRefinancing查找authorization_origin_id == auth_id,然后将该字段更新为相同的auth_id值。

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

目前尚不清楚你想在控制器的那一部分做什么,但我怀疑你想要将这两行压缩到HistoricRefinancing.create(authorization_origin_id: auth_id)