기록 : 내가 보장 할유효성 검사/어린이 나는이 개 모델이
Invoice has_many :lines
Line belongs_to :invoice
을 그 주어진 Invoice
경기 전체에 대한 Line
의 합 관련 Invoice
.
나는이 시도했다 :
validate :total_amount
def total_amount
inv_id = self.invoice_id
target_amount = Invoice.find(inv_id).total
total_lines = Line.where(invoice_id: inv_id).sum(:line_value)
errors.add(:total, " should be lower or equal to the total amount of the invoice") if total_lines > target_amount
end
그러나
- 는 새로운 객체 (단지 업데이트) 작동하지 않습니다 체계적에서 오류가 발생하더라도 업데이트가
AssociatedValidator에 관한 질문도 보았습니다.하지만 사용 방법을 파악하지 못했습니다. 그 :(