Girders Blog
Notes on building internet applications

Loading system libraries in Ruby on Rails (v3) Applications

Aug 6, 2010

I just discovered this little surprise working with an app on Rails 3.0.0.rc. I have a class

# app/models/account.rb
require 'Resolv'
class Account
  def verify_email
     mx = Resolv::DNS.open { |dns| dns.getresources(domain, Resolv::DNS::Resource::IN::MX) }
  end
end

This would work fine. The first time only. After that, it though an exception

  NameError: uninitialized constant Account::Resolv

Huh?

Running in the development environment, every time the web server reloads the class, or after the reload! command, it would remove the symbol and not require the library again. Or something like that.

What should you do?

Do not use the require statements in your rails class files. I moved all my require statements at the top of my config/application.rb file and all was better!