我有兩張桌子business_hours
和working_hours
.
型號:
class BusinessHour < ApplicationRecord
has_many :working_hours, class_name: "WorkingHour"
belongs_to :organization
accepts_nested_attributes_for(:working_hours, update_only: true)
end
class WorkingHour < ApplicationRecord
belongs_to :business_hour
validates :day, inclusion: { in: %w(mon tue wed thu fri sat sun) }
validate :validate_day, on: :create
def validate_day
if business_hour.working_hours.where(day: self.day).exists?
errors.add(:day, "has already been added")
end
end
end
控制器:
class Api::V1::Admin::BusinessHoursController < Api::BaseController
def update
@organization.build_business_hours unless @organization.business_hours
if @organization.business_hours.update(business_hour_params)
render status: :ok,
json: { notice: I18n.t("resource.update", resource_name: "Organization") }
else
render status: :unprocessable_entity, json: { errors: @organization.business_hours.errors.full_messages }
end
end
private
def business_hour_params
params.require(:business_hours).permit(
:enabled, :away_message, working_hours_attributes: [:day, :start_time, :end_time]
)
end
end
當business_hours
更新時,我working_hours
也在嘗試更新。
所需的行為是,working_hours
應該使用從mon
到的日期欄位創建friday
,每個business_hour
將有 7 個working_hours
條目。例如,如果 aworking_hour
已經存在一個 with day as "mon" business_hour
,當呼叫控制器中的更新方法時,只需要為特定的 更新start_time
and 。如何解決這個問題?end_time
working_hour
請求正文示例:
{
"business_hours": {
"enabled": true,
"away_message": "Hello",
"working_hours_attributes": [{
"day": "mon",
"start_time": "Tue, 06 Sep 2022 10:07:21.771116000 UTC 00:00",
"end_time": "Tue, 06 Sep 2022 10:07:21.771116000 UTC 00:00"
}]
}
}
uj5u.com熱心網友回復:
正如評論中所說:
好吧,可以使用“id”鍵更新嵌套屬性,如果您的前端沒有此資訊,您可以通過將“working_hours”存盤到鍵值對來處理要轉換的引數,...,我
day: 'mon'
不id: id
'認為沒有更好的方法可以在您的更新和允許的引數之間不使用此中間件
class Api::V1::Admin::BusinessHoursController < Api::BaseController
def update
@organization.build_business_hours unless @organization.business_hours
if @organization.business_hours.update(update_business_hour_params)
render status: :ok,
json: { notice: I18n.t("resource.update", resource_name: "Organization") }
else
render status: :unprocessable_entity, json: { errors: @organization.business_hours.errors.full_messages }
end
end
private
def business_hour_params
params.require(:business_hours).permit(
:enabled, :away_message, working_hours_attributes: [:day, :start_time, :end_time]
)
end
# THIS HASN'T BEEN TESTED, USE IT AS AN EXAMPLE
def update_business_hour_params
update_business_hour_params = business_hour_params
update_business_hour_params[:working_hours_attributes].each do |working_hour_parameters|
working_hour_parameters[:id] = working_hours_day_id_pair[working_hour_parameters.delete(:day)] # Retrieves the id from the day
end
update_business_hour_params
end
def working_hours_day_id_pair
@working_hours_day_id_pair ||= @organization.business_hours.working_hours.pluck(:day, :id).to_h
end
end
如前所述,這是一個示例,我無法測驗代碼,但這就是想法
由于您的屬性已經是 update_only,您應該一切順利,希望這對您有所幫助
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/507588.html