Reputation: 33552
I want to show an error message like "date of joining is Invalid" if the "date of joining" is before the "date of birth"(it doesnt happen in real.. but i want it).
Please suggest me with a piece of code.
Upvotes: 0
Views: 63
Reputation: 8442
i think you have a user model whith date_of_birth attribute , date of joining is the date when you save your user, and Active Record create automatically for you an attribute "created_at" for each object you save in database.
so you can consider "created_at" like date_of_join.
the next step is to add a validate method to your user model like this :
class User < ActiveRecord::Base
#attr_accessible :date_of_birth, ( etc ...... )
#.....
#.....
validate :date_of_join_must_be_great_than_birth
#.....
#.....
private
def date_of_join_must_be_great_than_birth
errors.add(:date_of_birth, "date of birth is greater than date of joining" ) unless date_of_birth < created_at
end
end
each time you save or you update your user, validate method is invoked
you can also learn about filter methods in rails , i hope this can help you
Upvotes: 1
Reputation: 1560
You can do this by many ways.Just refer this tutorial
http://guides.rubyonrails.org/active_record_validations_callbacks.html
Upvotes: 0