Rails Custom Validation

Sis Ccr
Apr 18, 2021

Writing custom validation was easy in rails

I had a hotel model and user model. User has many hotels. When creating hotel, I had to validate that it’s email is save as users email associated with it.

class Hotel < ApplicationRecord  validate :check_email?   def check_email?
errors.add :base, "Hotel email must be same as user's email!"
unless self.email == self.user.email
end
end

And that’s it. Please check the spelling of validate. I had some trouble figuring out what was wrong when I mistakely typed validates :check_email?

--

--