Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
##add to model (can be in protected section) def after_validation # Create an empty array to hold the field names fields = [] # Iterate through the errors errors.each do |field,message| # Unless you have alreayd processed the field add the errors (This is necessary to remove duplicates for the 'has many' fields) unless fields.include?(field) # If the field of an error is really an association, then the 'validates_associated' found an error if self.class.reflect_on_all_associations.collect(&:name).index(field.to_sym) # Iterate through the objects in the association looking for the invalid ones self.send(field).each do |association| if association and !association.valid? # add the error messages of the associated object to my error messages association.errors.each_full do |msg| self.errors.add_to_base "#{association.class.name}: #{msg}" end end errors.delete field end fields << field end end end end ##add to config/initializers/error_patches.rb class ActiveRecord::Errors def delete(attribute) @errors.delete attribute.to_s end end
This paste will be private.
From the Design Piracy series on my blog: