Method: Glue::Validation::ClassMethods#validate_related
- Defined in:
- lib/og/validation.rb
#validate_related(*params) ⇒ Object Also known as: validate_associated
Validate that a property relation is not nil. This works for all relations.
You should not put a validation in both related classes, because it can cause infinite looping.
The Og libraries are required for this method to work. You can override this method if you want to use another OR mapping library.
Example
class Uncle
has_many :secrets, Secret
:secrets
end
class Secret
belongs_to :uncle, Uncle
end
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/og/validation.rb', line 75 def *params c = parse_config(params, :msg => Glue::Validation::Errors.invalid_relation, :on => :save ) params.each do |field| define_validation(:related, field, c[:on]) do |obj| value = obj.send(field) if value.members.length == 0 obj.errors.add(field, c[:msg]) else valid = value.members.inject do |memo, rel_obj| (rel_obj.nil? or rel_obj.valid?) and memo end obj.errors.add(field, c[:msg]) unless valid end end end end |