Skip to content

Instantly share code, notes, and snippets.

@s-andringa
Created March 24, 2009 12:52
Show Gist options
  • Save s-andringa/84067 to your computer and use it in GitHub Desktop.
Save s-andringa/84067 to your computer and use it in GitHub Desktop.
class Person < ActiveRecord::Base
# If I18n.t is evaluated when the model is loaded, which in production happens only once
# the translation will always use the locale which was set at that moment.
#
# (Instead just use the activerecord.errors.person.attributes.email.invalid key in
# the translation files)
#
# This does NOT work properly:
validates_format_of :email, :with => REGEXP, :message => I18n.t(:email_invalid)
# Methods are evaluated on run time, so I18n.t works fine here:
def validate
errors.add(:email, I18n.t(:email_invalid)) unless email =~ REGEXP
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment