rails/rails — Issue #4326: Inflections fails on some irregular model names
Author: blakink
Created: 2012-01-05 16:37:22+00:00
State: open

So, a method _default_wrap_model is called when rails loads a controller.
If fails to determine the default model with an irregular singular and plural.

You can easily reproduce on a fresh app and :

* a model `NewsItem`
* a controller `NewsController`
* an inflection `inflect.irregular 'news_item', 'news'`

Then, when calling the controller, you'll get the following error :

> Admin::NewsController
LoadError: Expected [...]/app/models/news_item.rb to define News_item
from [...]/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:490:in `load_missing_constant'
from [...]/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:181:in `block in const_missing'
from [...]/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:179:in `each'
from [...]/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:179:in `const_missing'
from [...]/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:501:in `load_missing_constant'
from [...]/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:181:in `block in const_missing'
from [...]/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:179:in `each'
from [...]/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:179:in `const_missing'
from [...]/gems/activesupport-3.1.3/lib/active_support/inflector/methods.rb:124:in `block in constantize'
from [...]/gems/activesupport-3.1.3/lib/active_support/inflector/methods.rb:123:in `each'
from [...]/gems/activesupport-3.1.3/lib/active_support/inflector/methods.rb:123:in `constantize'
from [...]/gems/activesupport-3.1.3/lib/active_support/core_ext/string/inflections.rb:43:in `constantize'
from [...]/gems/actionpack-3.1.3/lib/action_controller/metal/params_wrapper.rb:148:in `_default_wrap_model'
from [...]/gems/actionpack-3.1.3/lib/action_controller/metal/params_wrapper.rb:167:in `_set_wrapper_defaults'
from [...]/gems/actionpack-3.1.3/lib/action_controller/metal/params_wrapper.rb:128:in `inherited'
from [...]/gems/actionpack-3.1.3/lib/abstract_controller/railties/routes_helpers.rb:7:in `block (2 levels) in with'
from [...]/gems/actionpack-3.1.3/lib/action_controller/railties/paths.rb:7:in `block (2 levels) in with'
from [...]/app/controllers/admin/news_controller.rb:1:in `'
...

So, the problem seems to be around the load of model.
I can also reproduce that without controller :

> NewsItem
=> NewsItem(Table doesn't exist)
> News_item
LoadError: Expected /Users/admin/dev/thienpont/foo/app/models/news_item.rb to define News_item
from [...]/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:490:in `load_missing_constant'
from [...]/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:181:in `block in const_missing'
from [...]/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:179:in `each'
from [...]/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:179:in `const_missing'

I guess we should get the error `NameError: uninitialized constant News_item`, shouldn't we ?