我真的需要签if shift
入我的自定义验证器吗?validates :shift, presence: true
我可以以某种方式重构它吗?
class ShiftLog < ActiveRecord::Base
belongs_to :shift
validates :shift, presence: true
validate :check_limit
def check_limit
if shift
shift_logs = ShiftLog.by_shift(shift)
if shift_logs.count >= self.shift.limit
errors.add(:shift_id, "Exceeded limit")
end
end
end
end