I am trying to create a registration module where a user can register only if their email is already in the existing database.
Models:
The condition for the user will be
if OldUser.find_by_email(params[:UserName]) exists, allow user registration.
If not, then indicate error message.
This is really easy to do in PHP, where I can simply run the function to execute the mysql query. However, I could not figure out how to do this on Rails. It seems like I should create a custom validator function, but it seems too overpriced for such a simple condition.
It should be pretty simple. What did I miss?
Any pointer?
Edit 1:
This dku.rajkumar solution works with a slight modification:
validate :check_email_existence
def check_email_existence
errors.add(:base, "Your email does not exist in our database") if OldUser.find_by_email(self.UserName).nil?
end
For such cases, is it better to check in the model or controller?