# Complex Error Handling

**URL:** <https://www.ruby-forum.com/t/complex-error-handling/115158>\
**Category:** Rails\
**Created:** [September 21, 2007, 5:53am UTC](https://www.ruby-forum.com/t/complex-error-handling/115158 "2007-09-21T05:53:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Curtis\_Hawthorne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecae2f/32.png) [@Curtis\_Hawthorne](https://www.ruby-forum.com/u/Curtis_Hawthorne)\
**Post date:** [September 21, 2007, 5:53am UTC](https://www.ruby-forum.com/t/complex-error-handling/115158/1 "2007-09-21T05:53:37Z")

</div>

I’ve got a model that has to check other objects before it is valid.  
If there are problems with the other objects, I want to report that  
and also give the template access to the offending objects. The  
regular errors object doesn’t seem suited to this. Is there another  
way?

Here’s a better explanation of what’s going on:

Users are allowed to enter “events”. If they would like to, they can  
associate these events with a “trip”. However, if they do that, the  
time range specified for the trip needs to include the time of each of  
the individual events.

So, if the user goes to edit a trip and changes the time range, I need  
to go through all the events associated with the trip and make sure  
the times fall within the new range. If an event has a time outside  
that range, I’d like to give the user an error message letting them  
know what happened and even let them edit the event on the spot. So,  
I need a way to pass the actual event object (so I can get to all of  
its attributes, including ID) to the template, not just text about it.

Is there a good way to do this? I’m not even sure how to pass  
something more complex out of the model than just the standard text  
message.

Thanks!

Curtis H.

---

<div class="post-metadata">

**Author:** ![Curtis\_Hawthorne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecae2f/32.png) [@Curtis\_Hawthorne](https://www.ruby-forum.com/u/Curtis_Hawthorne)\
**Post date:** [September 21, 2007, 1:16pm UTC](https://www.ruby-forum.com/t/complex-error-handling/115158/2 "2007-09-21T13:16:21Z")

</div>

Curtis Hawthorne wrote:

> I’ve got a model that has to check other objects before it is valid.  
> If there are problems with the other objects, I want to report that  
> and also give the template access to the offending objects. The  
> regular errors object doesn’t seem suited to this. Is there another  
> way?

You could use validates\_associated for that purpose, and then inspect  
event.errors or trip.errors to see what the issues are.

Setting the necessary variables for the view to display what’s going on  
is best done in the controller, and I can recommend implementing the  
time span stuff in the models.

–  
Roderick van Domburg  
[http://www.nedforce.com](http://www.nedforce.com)
