在我的項目中,我有一個名爲PaymentCondition
的模型,另一個名爲PaymentPortion
。 PaymentCondition
has_manypayment_portions
and PaymentPortion
belongs_topayment_condition
。使用新的屬性值來驗證before_update
當我創建一個新PaymentCondition
,我有這樣的方法創建ñpayment_portions
。正在n從PaymentCondition
的:amount
屬性的值。
如果我創建一個新的PaymentCondition
與amount: 2
,例如,我會有2 payment_portions
。
到目前爲止,這麼好。
我的問題:
我使用的是嵌套形式這種觀點,所以我可以一次編輯一切。
PaymentCondition
有一個屬性catchments
。
PaymentPortion
有一個屬性,稱爲catchment
。
以前 submiting這種形式,我想檢查總和PaymentPortion.catchment
等於PaymentCondition.catchments
。如果不是,我必須提出一個錯誤。
至於現在,我不能在保存之前得到的PaymentCondition
新值...
我做這裏面payment_conditions_controller
:
before_update :check_catchments
def check_catchments
errors.add(:catchments, "Values must check") unless catchments_check? || new_record?
end
def catchments_check?
catchment == portion_catchments
end
def portion_catchments
payment_portions.sum(:catchments)
end
使用總和(),我只獲取數據庫中的值。我需要的是正在發送的值...
有沒有辦法做到這一點?
在此先感謝。
這是我首先想到的,實際上。第二個將在視圖上使用javascript進行此驗證...由於所有其他驗證都是在模型內部進行的,因此我認爲會有一些方法可以執行此操作。 – ThalesMiguel