Multiple instance of same AR validation error - Rails 1.2 RC

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

I have a custom implementation for the callback used in an
This is tested in my functional tests, including a specific test to
that the error message from the AR validation failure is displayed.
Unfortunately, I get an error on this code now, because the ActiveRecord
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

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

Thank you,


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 = = params[:value]!
rescue StandardError => name_error
logger.error(“Error setting search name \n#{name_error.inspect}”) = old_name
flash[:notice] = 'Search name ’ + @current_search.errors[‘name’] #
Barfs here, gets an Array of 2 errors

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
get :master
old_name = @response.session[:user]
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(
assert @response.body.index(“Search name is too long”)

But in Rails 1.2 RC2, I get an Error:

  1. Error:
    TypeError: can’t convert Array into String

executing the “post” because @current_search.errors[‘name’]returns an
instead of a String. But the Array is just the same error, twice.
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
80 characters)”]

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?
class YourModel < ActiveRecord::Base
LOADED = true


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://

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


A more recent thread where this is brought up can be found at: http://

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