# Unable to display errors for multiple records

**URL:** <https://www.ruby-forum.com/t/unable-to-display-errors-for-multiple-records/54852>\
**Category:** Rails\
**Created:** [March 14, 2006, 8:53pm UTC](https://www.ruby-forum.com/t/unable-to-display-errors-for-multiple-records/54852 "2006-03-14T20:53:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicksnels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/aca169/32.png) [@nicksnels](https://www.ruby-forum.com/u/nicksnels)\
**Post date:** [March 14, 2006, 8:53pm UTC](https://www.ruby-forum.com/t/unable-to-display-errors-for-multiple-records/54852/1 "2006-03-14T20:53:59Z")

</div>

Hi,

I’m trying to save multiple records of the same model in one go. Which  
is no problem at all. Problem starts when I want to introduce error  
handling. The problem is that no record should be saved when one of the  
records contains an error. I tried it as follows:

session[:infos].each do |info|  
@quest.name = info[“name”]

unless @quest.valid?  
@errors\_found = true  
end  
end

if @errors\_found  
render :action =\> ‘new’  
end

So if an error has occurred I prevent all the records from being saved.  
The only problem I’m having is that I can only show errors from the last  
record that contained an error. So if one or more of the other records  
has an error, I can’t show them to the user. Is their a way to  
‘preserve’ the errors added with errors.add in the model, so I can  
display the errors for all the records. All suggestions are welcome.

Kind regards,

Nick

---

<div class="post-metadata">

**Author:** ![nicksnels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/aca169/32.png) [@nicksnels](https://www.ruby-forum.com/u/nicksnels)\
**Post date:** [March 14, 2006, 9:21pm UTC](https://www.ruby-forum.com/t/unable-to-display-errors-for-multiple-records/54852/2 "2006-03-14T21:21:53Z")

</div>

There isn’t enough info to give a definitive answer to your problem,  
but if displaying the errors is the only problem then I suggest  
looking into using a custom error\_messages\_for in your helper.

# This example takes an array of objects and displays a single error box. Please excuse any ruby syntax errors, I am still learning the language.

def error\_messages\_for(object\_list, options = {})  
options = options.symbolize\_keys

# object\_list.each do |object| unless object.errors.empty? content\_tag(“div”, content\_tag( options[:header\_tag] || “h2”, “#{pluralize(object.errors.count, “error”)} prohibited this whatever from being saved” ) + content\_tag(“p”, “There were problems with the following fields:”) + content\_tag(“ul”, object.errors.full\_messages.collect { |msg| content\_tag(“li”, msg) }), “id” =\> options[:id] || “errorExplanation”, “class” =\> options[:class] || “errorExplanation” ) end end end

You can customize this all you want to add more details about each  
object.

-John

–  
John S.  
Computing Staff - Webmaster  
Kavli Institute for Theoretical Physics  
University of California, Santa Barbara  
removed\_email\_address@domain.invalid  
(805) 893-6307

---

<div class="post-metadata">

**Author:** ![nicksnels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/aca169/32.png) [@nicksnels](https://www.ruby-forum.com/u/nicksnels)\
**Post date:** [March 14, 2006, 9:54pm UTC](https://www.ruby-forum.com/t/unable-to-display-errors-for-multiple-records/54852/3 "2006-03-14T21:54:17Z")

</div>

I see what you are talking about. My last reply did not address the  
real problem.

The problem is as soon as an error is found validation stops for all  
further objects so a comprehensive validation cannot happen. I have a  
single record with a has\_many relationship and if an error is found  
with the original record then validation does not complete for the  
child records.

I have seen this limitation in other MVC frameworks (WebObjects). I  
need a solution to this also.

-John

–  
John S.  
Computing Staff - Webmaster  
Kavli Institute for Theoretical Physics  
University of California, Santa Barbara  
removed\_email\_address@domain.invalid  
(805) 893-6307

---

<div class="post-metadata">

**Author:** ![nicksnels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/aca169/32.png) [@nicksnels](https://www.ruby-forum.com/u/nicksnels)\
**Post date:** [March 14, 2006, 10:22pm UTC](https://www.ruby-forum.com/t/unable-to-display-errors-for-multiple-records/54852/4 "2006-03-14T22:22:33Z")

</div>

John,

thanks for your answer. It is funny, I had already implemented your  
first suggestion to solve the problem of displaying errors for multiple  
models. Took a while to find that one. And now as you describe it in  
your last response, is exactly the problem I’m having (and apparently  
other people too). So it would be really nice if somebody could provide  
a solution or at least some helpful comments.

Kind regards,

Nick

---

<div class="post-metadata">

**Author:** ![nicksnels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/aca169/32.png) [@nicksnels](https://www.ruby-forum.com/u/nicksnels)\
**Post date:** [March 15, 2006, 8:49am UTC](https://www.ruby-forum.com/t/unable-to-display-errors-for-multiple-records/54852/5 "2006-03-15T08:49:37Z")

</div>

Hi Bob,

I think I’m already doing it, see my code snippet at the top. The only  
thing is that I’m looping through it multiple times. And all the  
previous errors that are thrown are lost, only the errors on the last  
record (with errors) are preserved. This is standard behaviour, but  
maybe there is a way to override this? Thanks.

Kind regards,

Nick

---

<div class="post-metadata">

**Author:** ![nicksnels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/aca169/32.png) [@nicksnels](https://www.ruby-forum.com/u/nicksnels)\
**Post date:** [March 15, 2006, 4:57am UTC](https://www.ruby-forum.com/t/unable-to-display-errors-for-multiple-records/54852/6 "2006-03-15T04:57:42Z")

</div>

Quick answer is step in front of AR with your validations using valid?  
in your controller before you save it.

Bob S.  
[http://www.railtie.net/](http://www.railtie.net/)
