# Multiple instance of same AR validation error - Rails 1.2 RC

**URL:** <https://www.ruby-forum.com/t/multiple-instance-of-same-ar-validation-error-rails-1-2-rc/81412>\
**Category:** Rails\
**Created:** [January 8, 2007, 10:38pm UTC](https://www.ruby-forum.com/t/multiple-instance-of-same-ar-validation-error-rails-1-2-rc/81412 "2007-01-08T22:38:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lori](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8dc957/32.png) [@lori](https://www.ruby-forum.com/u/lori)\
**Post date:** [January 8, 2007, 10:38pm UTC](https://www.ruby-forum.com/t/multiple-instance-of-same-ar-validation-error-rails-1-2-rc/81412/1 "2007-01-08T22:38:51Z")

</div>

I’m working my way through testing errors with Rails 1.2RC2. This is  
the  
second of my problems.

I have a custom implementation for the callback used in an  
InPlaceEditor.  
This is tested in my functional tests, including a specific test to  
ensure  
that the error message from the AR validation failure is displayed.  
Unfortunately, I get an error on this code now, because the ActiveRecord  
is  
reporting 2 errors, instead of 1, and it is the same error.

“is too long (maximum is 80 characters)” \<— This error appears twice.

This is a bug, right? It doesn’t happen in Rail 1.1.6

Regards, Lori

---

<div class="post-metadata">

**Author:** ![lori](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8dc957/32.png) [@lori](https://www.ruby-forum.com/u/lori)\
**Post date:** [January 8, 2007, 11:55pm UTC](https://www.ruby-forum.com/t/multiple-instance-of-same-ar-validation-error-rails-1-2-rc/81412/2 "2007-01-08T23:55:59Z")

</div>

Hi. could you post all the relevant code for others to take a look at?

Thank you,

-Conrad

---

<div class="post-metadata">

**Author:** ![lori](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8dc957/32.png) [@lori](https://www.ruby-forum.com/u/lori)\
**Post date:** [January 9, 2007, 12:42am UTC](https://www.ruby-forum.com/t/multiple-instance-of-same-ar-validation-error-rails-1-2-rc/81412/3 "2007-01-09T00:42:02Z")

</div>

My model has one validation on the attribute in question:

validates\_length\_of :name, :in =\> 1…80

This is the callback in my controller:

def set\_current\_search\_name  
@current\_search = session[:user].current\_search  
old\_name = @current\_search.name  
@current\_search.name = params[:value]  
@current\_search.save!  
rescue StandardError =\> name\_error  
logger.error(“Error setting search name \n#{name\_error.inspect}”)  
@current\_search.name = old\_name  
flash[:notice] = 'Search name ’ + @current\_search.errors[‘name’] #  
Barfs here, gets an Array of 2 errors  
end

This is the functional test method, which just makes sure that the error  
handling works as I want it to:

def test\_search\_name\_too\_long  
login  
get :master  
old\_name = @response.session[:user].current\_search.name  
post :set\_current\_search\_name, { :value =\> “this name is too long  
because it is way more than 80 characters which is the allowed limit for  
search names” }  
assert\_equal old\_name, Search.find(  
@response.session[:user].current\_search.id  
).name  
assert @response.body.index(“Search name is too long”)  
end

But in Rails 1.2 RC2, I get an Error:

1. Error:  
test\_search\_name\_too\_long(SearchControllerTest):  
TypeError: can’t convert Array into String

executing the “post” because @current\_search.errors[‘name’]returns an  
Array,  
instead of a String. But the Array is just the same error, twice.  
Throwing  
in this line to see what the array holds:

```
puts "-----> " + @current_search.errors['name'].inspect

```

Shows me this:

-----\> [“is too long (maximum is 80 characters)”, “is too long (maximum  
is  
80 characters)”]

---

<div class="post-metadata">

**Author:** ![lori](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8dc957/32.png) [@lori](https://www.ruby-forum.com/u/lori)\
**Post date:** [January 31, 2007, 7:19am UTC](https://www.ruby-forum.com/t/multiple-instance-of-same-ar-validation-error-rails-1-2-rc/81412/4 "2007-01-31T07:19:34Z")

</div>

This is a problem in ruby. There is a bug up to 1.8.5 where  
Kernel.require doesn’t expand the path of a file it loads.

For example the below three requires will all load “file.rb”:  
require ‘file’  
require ‘./file’  
require ‘dir/…/file’

This can happen with tests in Rails easily if you use fixture data  
that contains erb tags which force the loading of an ActiveRecord  
model, and you refer to it using the “fixtures :data”.

You can avoid it with a cheap work around (I’m not a big fan of it) by  
wrapping your model class in a unless/end block:

unless Object.const\_defined?( :YourModel ) and YourModel.const\_defined?  
( :LOADED )  
class YourModel \< ActiveRecord::Base  
LOADED = true  
…  
end

This will make it so your “YourModel” class will only get loaded 1  
time from this this. The original thread on this was from 2001, http://  
blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/20198

It doesn’t claim it’s a bug in ruby, but it seems like a bug to me.

Zach

---

<div class="post-metadata">

**Author:** ![lori](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/8dc957/32.png) [@lori](https://www.ruby-forum.com/u/lori)\
**Post date:** [January 31, 2007, 7:25am UTC](https://www.ruby-forum.com/t/multiple-instance-of-same-ar-validation-error-rails-1-2-rc/81412/5 "2007-01-31T07:25:34Z")

</div>

A more recent thread where this is brought up can be found at: http://  
[groups.google.com/group/comp.lang.ruby/browse\_thread/thread/](http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/)  
ef3eaad6be70c7b6/a46649105aa21ecd?lnk=gst&q=require  
+bug&rnum=1#a46649105aa21ecd

Although not everyone considers this a bug, it surely doesn’t seem  
intuitive.

Zach
