Custom Validation Check
<p>I have following Model for the Leave. What I am trying to do is to check the number of leave available based on taken leave type, numbers of leave taken and leave allocation. It seems like my 'check_leave' is not getting validated for some reason without any error. Appreciate any help!</p>

<pre><code>class Leave &lt; 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: -&gt; {self.number_of_days.present?}

protected

  def check_leave
    if self.leave_type == 0
      if ( self.number_of_days = self.number_of_days + LeaveAllocation.last.medical_leave_counter ) &gt; LeaveAllocation.last.medical_leave
        self.errors.add(:number_of_days, "Days exceeded the limit")
      end
    elsif self.leave_type == 1
      if ( self.number_of_days = self.number_of_days + LeaveAllocation.last.annual_leave_counter ) &gt; LeaveAllocation.last.annual_leave
        self.errors.add(:number_of_days, "Days exceeded the limit")
      end
    end
  end
end
</code></pre>