1

我正在尝试创建一个自定义验证来验证计划的 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' 

我尝试创建自定义验证器并使用私有验证方法无济于事。有人可以为我阐明这一点吗?

4

2 回答 2

9

尝试替换这个:

errors.add_to_base("Date ranges cannot overlap with another schedule")

有了这个:

errors.add(:base, "Date ranges cannot overlap with another schedule")
于 2012-04-13T21:53:57.207 回答
1

代替:

errors.add_to_base

尝试使用:

errors.add
于 2012-04-13T21:11:51.833 回答