I was running into an annoyance whereby I'd end up with duplicated validation messages from ActiveRecord. I tracked this down to the source reload feature of Ramaze, which was reloading my AR model classes each time I edited them with the dev server running, which has the unfortunate side effect of adding the validators again on top of the same ones that were there before. So each reload adds another set of validators and another duplicate validation message. This sort of thing (mainly class methods that add to a list) often requires special care when working with a source reloading system. In this case I've worked around it by resetting AR's internal validator list to empty before creating my validators, by directly accessing the @validate_callbacks instance variable. For example:

class Event < ActiveRecord::Base
    # Clear all AR validations, to avoid getting duplicates on source reload.
    @validate_callbacks = []
    validates_presence_of :name
    validates_presence_of :description
end

Leave a Reply

Your email address will not be published. Required fields are marked *