【发布时间】:2016-05-02 11:56:34
【问题描述】:
这是我的模型 -
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
当我尝试运行验证时,它似乎只检查第一个“0”,即使我将选择更改为“1”。任何帮助,将不胜感激!谢谢
【问题讨论】:
-
在创建和更新时,number_of_days 是字段名(整数)
-
你能发布完整的模型类吗? @肖恩林
-
更新完整模型
-
:if => "self.number_of_days.present?"为什么这里需要双引号? @肖恩林 -
单人还是双人都没区别。
标签: ruby-on-rails ruby-on-rails-4 activerecord