我正在尝试创建一个自定义验证来验证计划的 start_date 和 end_date 不与另一个计划重叠
class Schedule < ActiveRecord::Base
#has many scheduleTimes (fk in scheduleTime)
has_many :scheduletimes, :inverse_of => :schedule
validate :dateOverlaps?
scope :active, lambda {
where('? between start_date and end_date', Date.today)
}
def dateOverlaps?
results = ActiveRecord::Base.connection.execute("Select (start_date::DATE, end_date::DATE) OVERLAPS ('#{self.start_date}'::DATE, '#{self.end_date}'::DATE) from schedules;")
errors.add_to_base("Date ranges cannot overlap with another schedule") if results.first["overlaps"] == 't'
end
然而,这会导致
NoMethodError: undefined method `add_to_base'
我尝试创建自定义验证器并使用私有验证方法无济于事。有人可以为我阐明这一点吗?