これは私のモデルである -Railsの4 - カスタム検証エラー
class Leave < ActiveRecord::Base
belongs_to :staff
validates :staff, :leave_type, :start_date, :end_date, :number_of_days, :approved_by, presence: true
enum leave_type: {Medical: 0, Annual: 1, Urgent: 3, "Birth Leave": 4}
validate :check_leave, :if => "self.number_of_days.present?"
protected
def check_leave
if self.leave_type = 0
if (self.number_of_days + LeaveAllocation.last.medical_leave_counter) > LeaveAllocation.last.medical_leave
self.errors.add(:number_of_days, "Days exceeded the limit")
end
end
if self.leave_type = 1
if (self.number_of_days + LeaveAllocation.last.annual_leave_counter) > LeaveAllocation.last.annual_leave
self.errors.add(:number_of_days, "Days exceeded the limit")
end
end
end
end
私は検証を実行しようとすると、それだけで私は選択を「1」に変更しても小切手に「0」最初のものと思われます。どんな助けもありがとう!ありがとう
作成時および更新時に、number_of_daysはフィールド名(整数) –
完全モデルクラスを投稿できますか? @Sean Lin –
完全なモデル –