I’m trying to create a separate class to do validation so that about 10
models can use the same date validation. I have tried to use the
example from the agile book but it’s not working. Here’s what I have:
app/models/date_callbacks.rb
class DateCallbacks
def before_validation(model)
if model.start_date > Date.today
model.errors.add(:start_date, "must be before today")
end
if !model.end_date.nil?
if model.end_date > Date.today
model.errors.add(:end_date, "must be before today")
end
end
if !model.end_date.nil?
if model.end_date < model.start_date
model.errors.add(:end_date, "must be later than start date")
end
end
end
end
app/models/address_history.rb
class PostgradTraining < ActiveRecord::Base
before_validation DateCallbacks.new
…
end
the error i get is:
Callbacks must be a symbol denoting the method to call, a string to be
evaluated, a block to be invoked, or an object responding to the
callback method.
thanks for any help,
Nate