1
我試圖創建一個驗證的計劃的起始日期日期和結束日期不會與其他日程重疊的自定義驗證Rails 3的自定義驗證
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'
我曾嘗試創建一個自定義驗證器並使用私有驗證方法無濟於事。有人能爲我發光一些嗎?
只是一個簡短的評論,你的方法不尊重Ruby的約定。 「dateOverlaps」應該被命名爲「date_overlaps」。 – jhchabran 2013-08-23 17:03:16