# Deprecation warning I get but the error is more than that I think

**URL:** <https://www.ruby-forum.com/t/deprecation-warning-i-get-but-the-error-is-more-than-that-i-think/157311>\
**Category:** Rails\
**Created:** [January 9, 2009, 10:33am UTC](https://www.ruby-forum.com/t/deprecation-warning-i-get-but-the-error-is-more-than-that-i-think/157311 "2009-01-09T10:33:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bearblu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a6a055/32.png) [@bearblu](https://www.ruby-forum.com/u/bearblu)\
**Post date:** [January 9, 2009, 10:33am UTC](https://www.ruby-forum.com/t/deprecation-warning-i-get-but-the-error-is-more-than-that-i-think/157311/1 "2009-01-09T10:33:06Z")

</div>

The following code from the RailsSpace book:  
def correct\_error\_message(boundary, length)  
error\_messages = ActiveRecord::Errors.default\_error\_messages  
if boundary == :max  
sprintf(error\_messages[:too\_long], length)  
elsif boundary == :min  
sprintf(error\_messages[:too\_short], length)  
else  
raise ArgumentError, “boundary must be :max or :min”  
end  
end

resulted in the following error message.  
Loaded suite /home/rmartin/NetBeansProjects/TestForge/test/unit/  
user\_test  
Started  
DEPRECATION WARNING: ActiveRecord::Errors.default\_error\_messages has  
been deprecated. Please use I18n.translate  
(‘activerecord.errors.messages’)… (called from default\_error\_messages  
at /usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active\_record/  
validations.rb:24)  
…F…  
Finished in 0.201547 seconds.

1. Failure:  
test\_screen\_name\_length\_boundaries(UserTest)  
[/home/rmartin/NetBeansProjects/TestForge/test/unit/…/  
test\_helper.rb:72:in `assert_length' /home/rmartin/NetBeansProjects/TestForge/test/unit/user_test.rb: 39:in`test\_screen\_name\_length\_boundaries’  
/usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/  
active\_support/testing/setup\_and\_teardown.rb:94:in ` __send__' /usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/ active_support/testing/setup_and_teardown.rb:94:in`run’]:  
\<“is too short (minimum is {{count}} characters)”\> expected but was  
\<“is too short (minimum is 4 characters)”\>.

10 tests, 31 assertions, 1 failures, 0 errors

The deprecation warning I get but it doesn’t appear as though the  
error message is being manipulated correctly; any help out there?

---

<div class="post-metadata">

**Author:** ![bearblu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a6a055/32.png) [@bearblu](https://www.ruby-forum.com/u/bearblu)\
**Post date:** [January 17, 2009, 9:17pm UTC](https://www.ruby-forum.com/t/deprecation-warning-i-get-but-the-error-is-more-than-that-i-think/157311/2 "2009-01-17T21:17:11Z")

</div>

On Jan 9, 1:08 am, bearblu [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> …F…  
> /usr/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/  
> active\_support/testing/setup\_and\_teardown.rb:94:in `run’]:  
> \<“is too short (minimum is {{count}} characters)”\> expected but was  
> \<“is too short (minimum is 4 characters)”\>.
> 
> 10 tests, 31 assertions, 1 failures, 0 errors
> 
> The deprecation warning I get but it doesn’t appear as though the  
> error message is being manipulated correctly; any help out there?

I am getting the same problem

The deprecation warning is easy

in your user\_test.rb setup method replace the line:  
@error\_messages = ActiveRecord::Errors.default\_error\_messages  
with the line:  
@error\_messages = I18n.translate(‘activerecord.errors.messages’)

This is part of the internationalization of rails.

I still haven’t tracked down why sprintf wants a variable named count  
instead of just taking the variable in min\_length.  
For now I have performed the hack of commenting out the original line  
and adding a hand formatted line for the two min\_length tests:

```
#correct_error_message = sprintf(@error_messages[:too_short],

```

min\_length)  
correct\_error\_message = “is too short (minimum is #{min\_length}  
characters)”

and replace the three max\_length tests:

```
#correct_error_message = sprintf(@error_messages[:too_long],

```

max\_length)  
correct\_error\_message = “is too long (maximum is #{max\_length}  
characters)”

I would prefer to use the sprintf to format the message from  
the :too\_short symbol. It should change automatically if the  
default :too\_short message changes. I’ll live with the hack for now  
and chase down the change in sprintf in my spare time.

I’ll try to remeber to post the sprintf answer when i find it.

Sean

---

<div class="post-metadata">

**Author:** ![bearblu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a6a055/32.png) [@bearblu](https://www.ruby-forum.com/u/bearblu)\
**Post date:** [January 29, 2009, 10:28am UTC](https://www.ruby-forum.com/t/deprecation-warning-i-get-but-the-error-is-more-than-that-i-think/157311/3 "2009-01-29T10:28:19Z")

</div>

Well, it took me a while but I finally figured out the details of  
this.

First: As mentioned before this is caused by support for  
internationalization (Very Very cool feature) built into Rails 2.2.  
Read more (and do read it, or your 2.2 projects will not progress too  
far) at: [rails.info](http://guides.rails.info/i18n.html)

Second: First thing that needs to be done is to tell Rails where to  
find the “dictionary” for Active Record error messages, and (if you  
choose to use a locale other than en-US) also tell it to use the  
errors in our chosen locale. If you are using English, this is already  
done for you. Otherwise, read the URL above.

Third: Next you need to choose your message, and have i18n library do  
the translation and parameter filling for you. So all you need to do  
is to replace this line:  
correct\_error\_message = sprintf(@error\_messages[:too\_long],  
max\_length)  
with this line:  
correct\_error\_message = I18n.translate  
“activerecord.errors.messages.too\_long”, :count=\>max\_length  
That is it!  
You may ask, why should the internationalization gem do the parameter  
replacement? Think about it! The plural form of a noun, or the formate  
of date and time, or the symbols for currencies, even how numbers are  
written, all vary from language to language. So it’s best to fill the  
parameters right here.

Good Luck
