class Task < ActiveRecord::Base
validate :date_check
def date_check
if self.parent_id
errors.add(:due_on) if self.due_on > self.parent.due_on
else
errors.add_to_base("sub tasks have invalid dates") if
self.children.collect(&:due_on).any?{|date| date > self.due_on}
end
end
end
You can use the same function to validate that the child comes before
the parent AND that the parent doesn't contain any children that are
due after itself.
class Task < ActiveRecord::Base
validate :date_check
def date_check
if self.parent_id
errors.add(:due_on) if self.due_on > self.parent.due_on
else
errors.add_to_base("sub tasks have invalid dates") if
self.children.collect(&:due_on).any?{|date| date > self.due_on}
end
end
end
You can use the same function to validate that the child comes before
the parent AND that the parent doesn't contain any children that are
due after itself.