Zardoz Zardoz - 1 year ago 57
Ruby Question

Bundler.require does not work for ActiveRecord in my gem

I just created a new gem (using bundler) and want to add Active Record support. So I added

s.add_dependency "activerecord", "~> 3.0"
to my gemspec. Then I use Bundler.setup and Bundler.require and thought that I have access to Active Record now, but I haven't. I have to explicitly use
require "active_record"
. Any idea why Bundler.require does not work for me in that case?

Answer Source

Firstly, if you're packaging a gem, do not use Bundler.require. Bundler.require is for apps not gems.

  • In .gemspec, specify the dependencies of your deployed gem.

  • In your Gemfile, include the line gemspec to automatically include the dependencies listed in your .gemspec in your Gemfile.

    You may also optionally create gem groups for dev and test.

  • In your code, explicitly require any libraries you need.

I lost a couple of hours on this today so I hope this helps.

(Sources 1, 2)

Secondly, though the ActiveRecord gem is called "activerecord", the lib is called "active_record". This is what you would need in Gemfile.

gem 'activerecord', :require => "active_record"

Unless you include the :require option, ActiveRecord won't be loaded correctly and you won't know about it until you try to use it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download