Getting uninitialized constant error when trying to run tests

后端 未结 12 2240
礼貌的吻别
礼貌的吻别 2020-12-10 13:29

I just updated all my gems and I\'m finding that I\'m getting errors when trying to run Test::Unit tests. I\'m getting the error copied below. That comes from creating new,

12条回答
  •  没有蜡笔的小新
    2020-12-10 13:37

    I am not a nuby to rails but I am still learning and hopefully always will be :-).

    Rails 2.3 production environment using Ruby Enterprise Edition and passenger can produce a totally misleading useless error during startup (/var/log/passenger.log). Something like:

    Exception NameError in PhusionPassenger::Rack::ApplicationSpawner (uninitialized constant XXX)

    If you run script/console on the production server, you may see:

    Loading production environment (Rails 2.3.4)
    /home/ubuntu/MyApp/shared/bundle/ruby/1.8/gems/activesupport-2.3.4/lib/active_support/dependencies.rb:105:in  const_missing:NameError: uninitialized constant XXX
    

    This is happening to me in this instance only in production environment, not staging, not development. After a full day of research and experiments, I have concluded that in the production environment, REE or something must pre-load classes and the pre-loader apparently does not like to see classes being re-opened before they are created (an educated guess).

    In app/models/calendar.rb:

      Class Calendar < ActiveRecord::Base
        # This defines the class
      End
    

    In app/models/event.rb

      Class Event < ActiveRecord::Base
        # This desined the class
      End
      Class Calendar
        # This is supposed to 're-open' the Calendar class
        has_many :events
      end
    

    The above generic sample code snippet can cause startup issue. I am not sure of the order that pre-loading classes takes place but I suspect that may be the issue. I moved the ‘has_many :events’ into the class definition in app/modeles/calendar.rb and my app now starts without error.

    So, a good rule to follow is to put your Active Record Associations (has_many, belongs_to) inside the defining class (where the class is created).

提交回复
热议问题