Reputation: 97
I am new in Rails so please excuse this question. I recently ran into a problem and I would be grateful if someone could help finding the right solution.
I want to add this Custom Method to my validation. It should raise an error once the user submitd a start date that is past the end date
class Request < ActiveRecord::Base
validates :start_date, :to_date, presence: true
validates :checkin_date_cannot_be_in_the_past
def checkin_date_cannot_be_in_the_past
if :date > :todate
errors.add(:date, "can't be in the past")
end
end
Unfortunately this Custom Method doesn't work for me like described in the Active Record Validations documentation and raises an error (syntax error, unexpected end-of-input, expecting keyword_end) in my Controller:
def new
@request = Request.new
end
After spending some time researching I cant find the right solution so I hope maybe someone here could help?
Cheers
Rob
Upvotes: 1
Views: 825
Reputation: 3323
this should be validate for calling custom validation not validates
class Request < ActiveRecord::Base
validates :start_date, :to_date, presence: true
validate :checkin_date_cannot_be_in_the_past
def checkin_date_cannot_be_in_the_past
errors.add(:date, "can't be in the past") if date > todate
end
end
Upvotes: 2