Change or update an attribute value during Rails ActiveRecord validation

Viewed 6483

Summary: I'm trying to alter an attribute's value within a custom ActiveModel::EachValidator validator. Given the following prototype:

def validate_each(record, attribute, value)

trying to set value = thing doesn't appear to do anything -- am I missing something? There should be a smart way to do this...

Detail: I accept a URL input as part of a site. I don't want to just take the URL and directly validate that it returns a 200 OK message, because that would ignore entries that didn't start with http, or left out the leading www, etc. I have some custom logic to deal with those errors and follow redirects. Thus, I'd like the validation to succeed if a user types in example.org/article rather than http://www.example.org/article. The logic works properly inside the validation, but the problem is that if somebody types in the former, the stored value in the database is in the "wrong" form rather than the nicely updated one. Can I change the entry during validation to a more canonical form?

1 Answers
Related