私のプロジェクトでは、PaymentCondition
というモデルとPaymentPortion
というモデルがあります。 PaymentCondition
has_manypayment_portions
およびPaymentPortion
belongs_topayment_condition
。新しい属性値を使用してbefore_updateを検証する
私は新しいPaymentCondition
を作成するときに、私はn個payment_portions
を作成し、このメソッドを持っています。 nの値は、の値PaymentCondition
です。
たとえばと新しいPaymentCondition
を作成すると、2 payment_portions
になります。
これまでのところ、とても良いです。
私の問題:
私は、このビューにネストされたフォームを使用していますので、私は一度にすべてを編集することができます。
PaymentCondition
には、catchments
という属性があります。
PaymentPortion
には、catchment
という属性があります。
このフォームをsubmiting前に、私は合計PaymentPortion.catchment
のがPaymentCondition.catchments
に等しいかどうかを確認したいと思います。もしそうでなければ、私はエラーを起こさなければならない。
今のように、私は私がpayment_conditions_controller
内でこれをやっている
...それを保存する前にPaymentCondition
の新しい値を取得することはできません。
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
()、私はデータベース上の値だけを取得します。私が必要とするのは、送信されている値です...
どうすればいいですか?
ありがとうございます。
これは私が考えた最初の事実です。 2番目のビューはビュー上でjavascriptを使ってこのバリデーションを行うことです...他のすべてのバリデーションがモデル内で行われているので、私はこれを行う方法があると思いました。 – ThalesMiguel