I just created a new gem (using bundler) and want to add Active Record support. So I add开发者_Go百科ed 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?
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 linegemspec
to automatically include the dependencies listed in your.gemspec
in yourGemfile
.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.
If you want use Bundler you need define your Gemfile with Activerecord
gem 'activerecord', "~> 3.0.0"
Or you need define bundler to use your gemspec with adding gemspec in your Gemfile
gemspec
See http://gembundler.com/rubygems.html
I had this problem, and the issue in my case was that I was naming a directory in my gem active record, as in:
lib ->
active_record ->
base.rb <- containing some monkey patches to base
This was causing mass confusion including sweet error messages like:
Gem Load Error is: uninitialized constant ActiveRecord::Base
Did you mean? ActiveRecord::Base
Simply moving changing the file from lib/active_record/base.rb
to lib/active_record_base.rb
fixed it for me.
精彩评论