Hi!
I found that it's a popular use case for AR/AM validations:
class User < AR::Base
validates_numericality_of :age
#custom validation
validate :user_older_16
def user_older_16
errors.add(:age, "should be older 16") if age <= 16
end
end
When you will try to validate as the following:
user = User.new(age: 'bla')
user.valid?
you will get: ArgumentError: comparison of String with 16 failed.
Folks write a lot of code to check attribute values in their custom
validations, but it's not a DRY way because attributes already validated by
AM.
Is it a good idea to run validation methods only if errors.count == 0?
Or maybe we can add something like "validate_further :user_older_16" that
will run only if attributes are valid?
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Core" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-core/-/Ca3mpVVPWlgJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-core?hl=en.