Skip to content

Instantly share code, notes, and snippets.

@atnan
Created September 3, 2008 04:59
Show Gist options
  • Save atnan/8543 to your computer and use it in GitHub Desktop.
Save atnan/8543 to your computer and use it in GitHub Desktop.
# http://pivots.pivotallabs.com/users/nick/blog/articles/359-alias-method-chain-validates-associated-informative-error-message
# http://pastie.caboo.se/142774
module ActiveRecord::Validations::ClassMethods
def validates_associated(*associations)
associations.each do |association|
class_eval do
validates_each(associations) do |record, associate_name, value|
associates = record.send(associate_name)
associates = [associates] unless associates.respond_to?('each')
associates.each do |associate|
if associate && !associate.valid?
associate.errors.each do |key, value|
record.errors.add(key, value)
end
end
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment