Performance: Custom validation methods
MEDIUM IMPACT
Custom validation methods affect server-side processing time and can indirectly impact user experience by delaying form submission feedback.
class User < ApplicationRecord validates :email, format: { with: URI::MailTo::EMAIL_REGEXP } validates :email, uniqueness: true end
class User < ApplicationRecord validate :check_email_format def check_email_format # Inefficient regex and multiple DB calls if email.present? && !email.match?(/^[\w+\-.]+@[a-z\d\-.]+\.[a-z]+$/i) errors.add(:email, 'is invalid') end if User.where(email: email).exists? errors.add(:email, 'has already been taken') end end end
| Pattern | Server CPU Time | DB Queries | Response Delay | Verdict |
|---|---|---|---|---|
| Custom validation with DB calls | High | Multiple per validation | Increases response time | [X] Bad |
| Built-in validators | Low | Optimized single queries | Minimal delay | [OK] Good |