I have a Ruby on Rails project that has been untouched for a while, and I’m in the process of trying to upgrade it from Rails 2.0 to 3.1.
I’m getting an error when I try and instantiate one of the models. It seems as though one of the models is also defined somewhere as a module, and this is stopping me from instantiating it.
dgs@dgs-desktop ~/code/spelling $ rails c
Loading development environment (Rails 3.1.1)
ree-1.8.7-head :001 > Spelling.first
NoMethodError: undefined method `first' for Spelling:Module
from (irb):1
ree-1.8.7-head :002 > exit
The spelling class is very basic:
class Spelling < ActiveRecord::Base
belongs_to :word, :class_name => 'Word', :foreign_key => 'word_id'
end
I can’t find where in the app (which is pretty small) this module would be defined:
dgs@dgs-desktop ~/code/spelling $ cd app
dgs@dgs-desktop ~/code/spelling/app $ grep Spelling * -R
models/spelling.rb:class Spelling < ActiveRecord::Base
models/word.rb: has_many :spellings, :class_name => 'Spelling', :foreign_key => 'word_id'
models/spelling_user.rb:class SpellingUser < ActiveRecord::Base
views/layouts/application.html.erb: <title> School Spelling Tests</title>
dgs@dgs-desktop ~/code/spelling/app $ find ./ -name "spelling*"
./views/spellings
./views/admin/spellings
./models/spelling.rb
./models/spelling_user.rb
Does anyone know what could be causing this? Or how else I could track down where this module is being defined?
So . . . . after much hunting round in completely the wrong place, I found the (obvious) answer.
Somewhere along the upgrade from rails 2.x to rails 3.1 the application name becomes a module. As I had a model with the same name as the application, this model failed.
(I had seen this line in application.rb show up during my greps, but discounted it)
The rest of the application worked fine, it was only when I reached something depending on this model that it was failing. When I copied the entire application bit by bit into a temporary application (called
spelling_new) everything worked, so decided it must have been some cruft in the original application and renamedspelling_new->spelling. At this point everything blew up again and the culprit became clear.