0

在我的項目中,我有一個名爲PaymentCondition的模型,另一個名爲PaymentPortionPaymentConditionhas_manypayment_portions and PaymentPortionbelongs_topayment_condition使用新的屬性值來驗證before_update

當我創建一個新PaymentCondition,我有這樣的方法創建ñpayment_portions。正在nPaymentCondition:amount屬性的值。

如果我創建一個新的PaymentConditionamount: 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 

使用總和(),我只獲取數據庫中的值。我需要的是正在發送的值...

有沒有辦法做到這一點?

在此先感謝。

回答

0

可能直接在控制器中工作是一種選擇嗎?你可以使用像'build'或'first_or_initialize'這樣的方法來創建AR對象。然後在控制器中驗證它們,並在必要時保存數據。

+0

這是我首先想到的,實際上。第二個將在視圖上使用javascript進行此驗證...由於所有其他驗證都是在模型內部進行的,因此我認爲會有一些方法可以執行此操作。 – ThalesMiguel