更新父级并在Active Record中添加子级

时间:2017-01-27 06:54:03

标签: ruby-on-rails rails-activerecord

我获得了POST的JSON记录数组: -

我的父表模型如下: -

class UserVehicle < ApplicationRecord
  has_many    :service_bills
end

我的子表模型如下: -

class ServiceBill < ApplicationRecord
    belongs_to  :user_vehicle
end

这是我的控制器代码(更新父级和插入子级)

class Api::V1::ServiceBillsController < ApplicationController

  def create
    service_bill_params[:user_vehicles].each do |user_vehicle|
      @vehicle = UserVehicle.find_by(vehicle_number:  user_vehicle[:vehicle_number])

      if @vehicle
        user_vehicle[:transaction_details].each do |service_bill|
          @service_bill = ServiceBill.create(service_bill)
        end
      else
        p 'vehicle not found'
      end
    end

    begin
      if @service_bill.save
        format_response("Ok", 200, "Transaction details inserted successfully.", service_bill_params)
      end    
    rescue => e
      format_response("Error", 301, e, [])
    end

  end


  private
  def service_bill_params
    service_bill_params = params.require(:service_bill).permit(:user_external_reference, :membership_card_number, user_vehicles: [ :vehicle_number,  :mobile_number, :total_savings, :current_mileage_reading, :last_service_visit, :current_tier, :next_mileage_tier, transaction_details: [:transaction_id, :transaction_external_reference, :total_savings, :amount_paid, :total_bill_amount, :transaction_date] ])
  end
end

上面的代码什么也没做,终端立即说BEGIN和ROLLBACK。我假设我错过了与父母子女关系设计的东西?

2 个答案:

答案 0 :(得分:1)

您需要使用rails嵌套属性,传递父&amp;哈希中的孩子然后使用单个方法保存它们(保存)。您可以阅读有关此内容的rails文档(http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html)。

答案 1 :(得分:0)

class Api::V1::ServiceBillsController < ApplicationController

  def create

    service_bill_params[:user_vehicles].each do |user_vehicle|
      @vehicle = UserVehicle.find_by(vehicle_number:  user_vehicle[:vehicle_number])

      if @vehicle

        user_vehicle[:transaction_details].each do |service_bill|

          service_bill[:current_mileage_reading] = user_vehicle[:remaining_mileage_for_next_tier]
          service_bill[:dealer_identification] = 1234
          @vehicle.update(user_vehicle.except(:transaction_details))
          @vehicle.service_bills.create(service_bill)


        end
      else
        p 'vehicle not found'
      end
    end

    begin
      if @vehicle.save
        format_response("Ok", 200, "Transaction details saved successfully.", service_bill_params)

      end    
    rescue => e
      format_response("Error", 301, e, [])
    end



  end



  def format_response(status, code, message, data)
    render json: {
      status: status,
      status_code: code,
      status_message: message,
      data: data
    }
  end

  private
  def service_bill_params
    service_bill_params = params.require(:service_bill).permit(:user_external_reference, :membership_card_number, user_vehicles: [ :vehicle_number, :total_savings, :last_transaction_date, :current_tier, :remaining_mileage_for_next_tier, transaction_details: [:transaction_id, :transaction_external_reference, :total_savings, :amount_paid, :total_bill_amount, :transaction_date] ])
  end
end

这似乎有效。谢谢。