Multiple instance of same AR validation error - Rails 1.2 RC2

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

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

Thank you,

-Conrad

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)”]

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

A more recent thread where this is brought up can be found at: 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