用rails解决查询sql [英] Solve query sql with rails

查看:47
本文介绍了用rails解决查询sql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

表历史id authorization_origin_id1 22 3

你在下面的图片中看到的

这是我的 new.html.erb

<%= simple_form_for(@refinancing) do |f|%><div class="form-inputs"><%= f.hidden_​​field :employee_id,值:@employee.first.id %><%= f.input :contract_number %>

<h3>保留值</h3><table class="table table-condensed table-bordered table-striped"><头><th>授权id</th>第<第>个合同号个</thead><% @authorizations.each do |authorization|%><tr><td><%=authorization.id%></td><td><%=authorization.contract_number%></td></tr></tbody><%结束%><div class="form-actions"><%= f.button :submit, "To Reserve" %>

<%结束%>

这是我的控制器:

def new如果 params[:authorization].present?@selected_ids = params[:authorization][:contract_number]@authorizations = Authorization.where("contract_number in (?)", @selected_ids)auth_params = params[:授权]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, 情况:2)结尾@authorizations.ids.each 做 |auth_id|@historic_refinancing = HistoricRefinancing.create@historic_refinancing = HistoricRefinancing.where(authorization_origin_id: auth_id).update_all(authorization_origin_id: auth_id)结尾结尾@employee = Employee.search_cpf(params[:search_employee_by_cpf])@refinancing = Refinancing.new结尾

目前,出现此错误:

authorization_origin_id 为空,请问我是怎么得到的?

解决方案

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

HistoricRefinancing.create 创建一个 Authorization_origin_id、refinancing_id 和 authorization_new_id 为空的条目.

下面的代码什么都不做,它尝试使用 authorization_origin_id == auth_id 查找 HistoricRefinancing ,然后将该字段更新为相同的 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)

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?

解决方案

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)

这篇关于用rails解决查询sql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆