Solve query sql with rails

微笑、不失礼 提交于 2020-01-26 04:39:04

问题


I need that when I create a Historic do it:

Table historic
id authorization_origin_id
1   2
2   3

How you see in image bellow

This is my 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 %>

And this is my controller:

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

For now, is appears this error:

The authorization_origin_id has null, please how I get this?


回答1:


In your controller, you're creating HistoricRefinancing without any parameters.

HistoricRefinancing.create creates an entry with null authorization_origin_id, refinancing_id, and authorization_new_id.

The code below does nothing, it attempts to find HistoricRefinancings with authorization_origin_id == auth_id and then update that field to the same auth_id value.

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

It's not clear exactly what you want to do in that part of the controller, but I suspect you want to condense both lines down to HistoricRefinancing.create(authorization_origin_id: auth_id)



来源:https://stackoverflow.com/questions/36874731/solve-query-sql-with-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!