# How to check that all children dates are before parent date?

**URL:** <https://www.ruby-forum.com/t/how-to-check-that-all-children-dates-are-before-parent-date/137218>\
**Category:** Rails\
**Created:** [May 10, 2008, 7:38am UTC](https://www.ruby-forum.com/t/how-to-check-that-all-children-dates-are-before-parent-date/137218 "2008-05-10T07:38:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [May 10, 2008, 7:38am UTC](https://www.ruby-forum.com/t/how-to-check-that-all-children-dates-are-before-parent-date/137218/1 "2008-05-10T07:38:53Z")

</div>

Hello,

I have a Task model that uses acts\_as\_tree, giving me the parent\_id. The  
Task table has these columns:

1. name
2. due\_on
3. parent\_id

How would I validate that all children due dates (due\_on) are inputted  
before the parent task due date before it’s saved?

(for example:  
“debug project” due on May 1st - children task  
“finish project” due on May 31st - parent task)

I’m thinking it might be something like this, but I’m sure I’m wrong:

class Task \< ActiveRecord::Base  
acts\_as\_tree  
validate :children\_date\_before\_parent\_date

```
def children_date_before_parent_date
  errors.add_to_base("Children date must be before parent date") if

```

due\_on \<= parent.due\_on  
end  
end

Any ideas?

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [May 10, 2008, 7:39pm UTC](https://www.ruby-forum.com/t/how-to-check-that-all-children-dates-are-before-parent-date/137218/2 "2008-05-10T19:39:41Z")

</div>

Try something like this:

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.

On May 10, 12:38 am, Bob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [May 10, 2008, 7:58pm UTC](https://www.ruby-forum.com/t/how-to-check-that-all-children-dates-are-before-parent-date/137218/3 "2008-05-10T19:58:38Z")

</div>

Andrew B. wrote:

> Try something like this:
> 
> 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.
> 
> On May 10, 12:38ï¿½am, Bob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

It works perfectly! Thank you so much for your awesome help, Andrew. I  
can’t thank you enough. THANK YOU 🙂
