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 に答える