Ruby on rails 在活动记录中更新父项并添加子项
我为POST获取了一个JSON记录数组:- 我的父表模型如下所示:-Ruby on rails 在活动记录中更新父项并添加子项,ruby-on-rails,rails-activerecord,Ruby On Rails,Rails Activerecord,我为POST获取了一个JSON记录数组:- 我的父表模型如下所示:- class UserVehicle < ApplicationRecord has_many :service_bills end class ServiceBill < ApplicationRecord belongs_to :user_vehicle end class UserVehicle
class UserVehicle < ApplicationRecord
has_many :service_bills
end
class ServiceBill < ApplicationRecord
belongs_to :user_vehicle
end
class UserVehicle
我的子表模型如下所示:-
class UserVehicle < ApplicationRecord
has_many :service_bills
end
class ServiceBill < ApplicationRecord
belongs_to :user_vehicle
end
class-ServiceBill
这是我的控制器代码(用于更新父级并插入子级)
class Api::V1::ServiceBillsControllere
格式_响应(“错误”,301,e,[])
结束
结束
私有的
def服务账单参数
服务账单参数=参数。要求(:服务账单)。许可证(:用户外部参考,:会员卡号码,用户车辆:[:车辆号码,:手机号码,:总储蓄,:当前里程阅读,:最后一次服务访问,:当前层,:下一层,交易详细信息:[:交易\ id、:交易\外部\参考、:储蓄总额、:支付金额、:账单总额、:交易\日期]]
结束
结束
上面的代码什么都不做,终端说立即开始并回滚。我假设我错过了父子关系设计的某些内容?您需要使用rails嵌套属性,以散列形式传递父子关系,然后使用单个方法保存它们(save)。您可以阅读rails文档()关于这一点。
类Api::V1::ServiceBillsControllerclass 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
def创建
服务账单参数[:用户车辆]。每个do用户车辆|
@车辆=用户车辆。查找方式(车辆编号:用户车辆[:车辆编号])
如果@车辆
用户|车辆[:交易|详细信息]。每个do |服务|账单|
服务费账单[:当前里程数读数]=用户车辆[:下一层剩余里程数]
服务单[:经销商识别]=1234
@车辆更新(用户车辆除外(:交易详细信息))
@车辆.服务账单.创建(服务账单)
结束
其他的
p“未找到车辆”
结束
结束
开始
如果@vehicle.save
格式\响应(“确定”,200,“成功保存交易明细”,服务\账单\参数)
结束
救援=>e
格式_响应(“错误”,301,e,[])
结束
结束
def格式_响应(状态、代码、消息、数据)
呈现json:{
状态:状态,
状态代码:代码,
状态消息:消息,
数据:数据
}
结束
私有的
def服务账单参数
服务账单参数=参数。要求(:服务账单)。许可(:用户外部参考,:会员卡号码,用户车辆:[:车辆号码,:总储蓄,:上次交易日期,:当前交易层,:下一层剩余里程,\交易详情:[:交易\ id、:交易\外部\参考、:储蓄总额、:支付金额、:账单总额、:交易\日期]]
结束
结束
这似乎奏效了。谢谢。在此处添加一些执行此任务的代码。堆栈溢出我们希望您在来寻求帮助之前尝试编写代码。如果您已经这样做了,那么您应该将该代码放入您的问题中(即使它不起作用)所以我们可以看到你已经投入了努力,然后我们可以帮助你调试坏代码。如果你没有,试一下-看看你能走多远。试着先做你知道怎么做的部分,为你不知道的部分留出空间…让这些部分工作,然后尝试你不确定的部分。然后回来帮助我们很抱歉,我很快会添加代码和确切的场景。