I am trying to convert an integer to a DAYNAME in my validation message
validates_uniqueness_of :day, scope: :store, :message => “already has a target for #{Date::DAYNAMES[self.day]}.”
``
This does not seem to work, I cannot access the day value.
The following displays the correct value
validates_uniqueness_of :day, scope: :store, :message => “already has a target for %{value}.”
``
This throws an error:
validates_uniqueness_of :day, scope: :store, :message => “already has a target for #{Date::DAYNAMES[%{value}]}.”
``
Any help would be Amazing!
Using double quotes with #{} will definitely not do what you want; the interpolation will be attempted at class-load time, not when the string is needed.
The value passed to :message is handed off to I18n.translate, so the docs for that may be some help.
It may be easier (and clearer) to just define a custom validation function that does what you want.
–Matt Jones
This is along the lines of what Matt is referring to.
class DayValidator < ActiveRecord::Validator
def validate()
if YOUR_TEST_FOR_INVALIDITY(record.day)
date = Date::DAYNAMES[record.day]
record.errors[:day] << “already has a target #{date}”
end
end
end
class YourClass < ActiveRecord::Base
validates_with DayValidator
end
``