2016-04-26 89 views
0

我有這個問題...我需要保存我的表歷史與所有授權ID檢查,更新列authorization_origin_id並設置列refinancing_id創建的再融資ID。例如:我檢查授權的ID 2和3,所以我創建歷史與兩條線authorization_origin_id 2和3以及再融資ID將是1。我的代碼:表歷史與所有授權ids檢查更新列

我的觀點此它:

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

+0

是的,這就是答案http://stackoverflow.com/questions/36874731/solve-query-sql-with-rails/ 36875135#36875135 –

回答

0

答案是:

@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)