10

I set up a project level RVM gemset for a sinatra app I am starting that will connect to a local database with Active Record. In order to test it I tried to run the below test app:

test.rb

require 'rubygems' # may not be needed, depending on platform
require 'sinatra'
require 'activerecord'

class Article < ActiveRecord::Base
end

get '/' do
  Test.establish_connection(
    :adapter => "sqlite3",
    :database => "hw.db"
  )
  Test.first.content
end

(Taken from the answer to this question: What's the best way to talk to a database while using Sinatra?)

When I run ruby -rubygems test.rb I get this error:

/Users/[user]/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- activerecord (LoadError)

I've already installed the Active Record gem and it shows up in gem list and rvm current displays the correct gemset. I am new to RVM and I think this is something to do with the it not having the correct load path but I feel like I've set everything up correctly so I'd appreciate suggestions on what's wrong. Thanks.

Community
  • 1
  • 1
tks
  • 745
  • 9
  • 31

2 Answers2

17

As far as I can tell require 'activerecord' has been deprecated. Try using

require 'active_record'

instead.

  • 1
    Why they dont rename the gem to active_record? This made me lose a lot of time, :( – Rafael Oliveira Sep 25 '13 at 15:47
  • Gem names tend to not correlate to how they are loaded as dependencies. Just looking at my project's `Gemfile` I can see multiple conventions in place, none of which are consistent. For example, `capybara-webkit` VS `database_cleaner`. – Andreas Kavountzis Dec 12 '13 at 15:03
  • Don't forget to restart your server after changing to avoid getting the error persisting! – fagiani Jun 28 '17 at 15:05
0

If you have not already installed the activerecord gem, you will also get that error:

Open a command prompt and run these commands in the terminal:

#Find if the active record gem is already installed on your computer:
gem query --local

#See the downloadable gems, and see if activerecord is still available:
gem query --remote --name-matches activerecord

#Install your gem:
gem install --remote activerecord

#See if it installed successfully and is in the installed gem list:
gem query --local  

Here is some code that uses the ActiveRecord gem to see if everything is configured right:

#Ruby code
require 'active_record'
class Dog < ActiveRecord::Base
  has_many :dog_tags
end
puts "activerecord gem is installed";

If everything is working, it will print "activerecord gem is installed" without any errors.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335