mongoid/mongoid — Issue #1574: Mongoid dependency determine_model differs from ActiveSupport::Dependencies
Author: jonhyman
Created: 2012-01-09 23:55:05+00:00
State: open

I'm not sure where to file this bug (Rails or Mongoid) but a problem arises with ActiveSupport::Dependencies calling #underscore on a model name while Mongoid tries to figure it out using #camelize, which are not true opposites of each other.

Say I have a model, MBACandidate. If I make the file name m_b_a_candidate.rb, then Rails will not be able to load it in development because of line 493 in active_support/dependencies (version 3.1.3), which calls:
```ruby
qualified_name = qualified_name_for from_mod, const_name
# => Object
path_suffix = qualified_name.underscore
# => mba_candidate.rb

file_path = search_for_file(path_suffix)
# => nil
# This causes the problem.
```

If you look at the code for #underscore, they allow acronyms and even say:
```ruby
# As a rule of thumb you can think of +underscore+ as the inverse of +camelize+,
# though there are cases where that does not hold:
#
# "SSLError".underscore.camelize # => "SslError"
```

In Rails::Mongoid, line 90 (on 2.4.0), Mongoid does:
```ruby
# model_path is ["mba_candidate"]
klass = model_path.map { |path| path.camelize }.join('::').constantize
```

Which raises the "Expected #{Rails.root}/models/mba_candidate.rb to define MbaCandidate."

I can get around this by renaming my collection.