Ruby on Rails Monday, July 28, 2014

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




On Friday, July 25, 2014 9:21:08 PM UTC-6, Ricky Hopkins wrote:
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!

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/968ab5ce-dce8-4aa4-8db0-bc96f6b9eeaa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment