Trouble requiring a gem

I have a gem that I created myself, which works swimmingly in a normal
.rb script. The gem is called ‘vandelay’ and I can run a test script
like this with no error:

require ‘rubygems’
require ‘vandelay’

Vandelay::VandelayBase.establish_connection(…)

p Vandelay::Product.find(1234).name

But I run in to major trouble if I try to use this gem in a rails
application. For example, I have this test controller:

class MyTest < ApplicationController
def test
render_text Vandelay::Product.find_by_allegroId(176500).name
end
end

I also put this at the bottom of my environment.rb:

require ‘vandelay’
Vandelay::VandelayBase.establish_connection(…)

If I fire up WEBRick and hit the action once, it shows the proper
result. But the second and subsequent times, I get an error instead:

uninitialized constant Product

(A complete stack trace is at the end of this posting).

I can’t figure out why this works only on the first hit, but I can
restart the server and it works again just once every time.

I am able to use other Gems without trouble (I use ferret for example).
Do I have something wrong with my gem, perhaps?

Ruby 1.8.4, rails 1.1.6, Mac OS X 10.4.7.

Thanks,

Geoff

Stack Trace:

/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:123:in
const_missing' app/controllers/index_controller.rb:6:intest’

/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:123:in
const_missing' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:941:inperform_action_without_filters’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/filters.rb:368:in
perform_action_without_benchmark' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:69:inperform_action_without_rescue’
/usr/local/lib/ruby/1.8/benchmark.rb:293:in measure' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:69:inperform_action_without_rescue’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/rescue.rb:82:in
perform_action' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:408:inprocess_without_filters’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/filters.rb:377:in
process_without_session_management_support' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/session_management.rb:117:inprocess’
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/dispatcher.rb:38:in
dispatch' /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/webrick_server.rb:115:inhandle_dispatch’
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/webrick_server.rb:81:in
service' /usr/local/lib/ruby/1.8/webrick/httpserver.rb:104:inservice’
/usr/local/lib/ruby/1.8/webrick/httpserver.rb:65:in run' /usr/local/lib/ruby/1.8/webrick/server.rb:173:instart_thread’
/usr/local/lib/ruby/1.8/webrick/server.rb:162:in start_thread' /usr/local/lib/ruby/1.8/webrick/server.rb:95:instart’
/usr/local/lib/ruby/1.8/webrick/server.rb:92:in start' /usr/local/lib/ruby/1.8/webrick/server.rb:23:instart’
/usr/local/lib/ruby/1.8/webrick/server.rb:82:in start' /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/webrick_server.rb:67:indispatch’
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/commands/servers/webrick.rb:59
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
require' /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:inrequire’
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/commands/server.rb:30
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
require' /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:inrequire’
script/server:3

/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:123:in
const_missing' app/controllers/index_controller.rb:6:intest’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:941:in
perform_action_without_filters' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/filters.rb:368:inperform_action_without_benchmark’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:69:in
perform_action_without_rescue' /usr/local/lib/ruby/1.8/benchmark.rb:293:inmeasure’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/benchmarking.rb:69:in
perform_action_without_rescue' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/rescue.rb:82:inperform_action’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/base.rb:408:in
process_without_filters' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/filters.rb:377:inprocess_without_session_management_support’
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/session_management.rb:117:in
process' /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/dispatcher.rb:38:indispatch’
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/webrick_server.rb:115:in
handle_dispatch' /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/webrick_server.rb:81:inservice’
/usr/local/lib/ruby/1.8/webrick/httpserver.rb:104:in service' /usr/local/lib/ruby/1.8/webrick/httpserver.rb:65:inrun’
/usr/local/lib/ruby/1.8/webrick/server.rb:173:in start_thread' /usr/local/lib/ruby/1.8/webrick/server.rb:162:instart_thread’
/usr/local/lib/ruby/1.8/webrick/server.rb:95:in start' /usr/local/lib/ruby/1.8/webrick/server.rb:92:instart’
/usr/local/lib/ruby/1.8/webrick/server.rb:23:in start' /usr/local/lib/ruby/1.8/webrick/server.rb:82:instart’
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/webrick_server.rb:67:in
dispatch' /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/commands/servers/webrick.rb:59 /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:inrequire’
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:in
require' /usr/local/lib/ruby/gems/1.8/gems/rails-1.1.6/lib/commands/server.rb:30 /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:inrequire’
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:in
`require’
script/server:3

Geoff C. wrote:

I have revisited the problem after being away from it for a while. The
problem persists, but I have more information:

1: I’m using Ruby 1.8.4 and Rails 1.1.6 on Mac OS X

2: The Vandelay::Product object is an AR model class

3: The problem only happens in development. If I switch to production it
goes away.

I found this potentially related bug report:

http://dev.rubyonrails.org/ticket/2819

It looks to me like this bug was fixed, but I’m not sure how to tell if
the fix is actually in the current release or not. Should I be looking
to update to a non-stable release?

To recap my prior post, when I use my gem in a very simple action, it
works on the first page load, but fails every time therafter with an
‘uninitialized constant’ error.

Thanks,

Geof