I have to fields in database start_date and end_date. What I want to validate is that start_date must be less than end_date. I want the model level validation . Please help me.
Only way I know how to do this is to add your own validate method e.g.:
# in your model...
def validate
errors.add(:start_date, "must be less than the end date") if
start_date >= end_date
end
There might be a helper to do this, but at the time I wasn't aware of
one so this is how I did it
Hope this helps.
- Wayne
Thanks for your advice but I have tried that but it doesn't work. Let me clarify what I have in view file <label for='join_date'>Join Date:</label> <%=date_select :job_experience, :join_date, :start_year=>Time.now.year, :end_year=>1900, :order=>[:month,:year],:discard_type=>true,:day=>false, :include_blank=>true %>
<label for='left_date'>Left Date:</label> <%=date_select :job_experience, :left_date, :start_year=>Time.now.year, :end_year=>1900, :order=>[:month,:year],:discard_type=>true, :day=>false,:include_blank=>true %>
Then I write the following in my model
def validate errors.add_to_base("left date cannot be less than join date ") if left_date<join_date end
But the validation doesn't work. Please Rails Lover give me some suggestion.