1

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

3
  • you need to call this method checkin_date_cannot_be_in_the_past for validation and what is date & todate here ? Commented Apr 21, 2014 at 15:32
  • sry mate that was just a typo (just edited). I actually do call checkin_date_cannot_be_in_the_past Commented Apr 21, 2014 at 15:37
  • you have missing one end for if condition that's why your are getting syntax error Commented Apr 21, 2014 at 15:38

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.