1

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?

Dave Smylie
  • 2,663
  • 3
  • 25
  • 32

2 Answers2

2

Try this :

Spelling.ancestors

which will give you all the parent classes and modules, which will give you a clue where it is. If that doesn't work, looking into the load_path variable:

y $LOAD_PATH

it will give you a list of the paths, but you will have to find the one that conflicts with your code. It's quite a lot, but it shouldn't be hard because most of the gems are well namespaced, so most likely, it is a custom patch that is sitting somewhere.

ez.
  • 7,604
  • 5
  • 30
  • 29
  • Spelling.ancestors gives me a 1 item array: [Spelling]. Not too informative! I'll have a hunt round $LOAD_PATH and see what I can find. – Dave Smylie Jan 24 '12 at 22:30
  • No luck with $LOAD_PATH - ended up searching all thru the .rvm/ directory to be sure, but couldn't find any other reference to this. If I upgrade to ruby 1.9.2 would Object.source_location return the source of the module? or just the source of the class it was included into... – Dave Smylie Jan 24 '12 at 22:51
  • try the solution in this post - with __file__ or source_location, http://stackoverflow.com/questions/175655/how-to-find-where-a-method-is-defined-at-runtime, it didn't work for me, but I have 1.8.7 – ez. Jan 24 '12 at 23:20
-1

So . . . . after much hunting round in completely the wrong place, I found the (obvious) answer.

dgs@dgs-desktop /tmp/spelling $ cat config/application.rb 
require File.expand_path('../boot', __FILE__)

require 'rails/all'

module Spelling
  class Application < Rails::Application
  ...
  end 
end

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 renamed spelling_new -> spelling. At this point everything blew up again and the culprit became clear.

Dave Smylie
  • 2,663
  • 3
  • 25
  • 32