Created
September 3, 2008 04:59
-
-
Save atnan/8543 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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