💎 Ruby on Rails
Beginner
What are ActiveRecord validations?
Answer
ActiveRecord validations ensure that only valid data is saved to the database. Declared in the model with macros: validates :name, presence: true, validates :email, uniqueness: true, format: { with: URI::MailTo::EMAIL_REGEXP }, validates :age, numericality: { greater_than: 0 }. When you call model.save or model.valid?, Rails runs all validations. If any fail, save returns false, and errors are accessible via model.errors.full_messages. Use save! or create! to raise an exception on failure instead.