Skip to content

Instantly share code, notes, and snippets.

@joho
Created February 17, 2009 00:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joho/65480 to your computer and use it in GitHub Desktop.
Save joho/65480 to your computer and use it in GitHub Desktop.
shitty helper method for cleaning up multi model's error messages (rails 1.2 blergh)
def tidy_error_messages(model, included_model_names)
new_errors = ActiveRecord::Errors.new(nil)
model.errors.each do |key, message|
new_errors.add(key, message) unless included_model_names.include? key
end
# re-jig the errors for the included models
for included_model_name in included_model_names
for included_model in model.send(included_model_name)
included_model.errors.each do |key, message|
# use a custom error message if the model supports it
if included_model.respond_to? :create_error_message
included_model.create_error_message(new_errors, key, message)
else
new_errors.add(included_model_name, "#{key} #{message}")
end
end
end
# wipe out the old errors and chuck the new ones in
model.errors.clear
new_errors.each do |key, message|
model.errors.add key, message
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment