Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uninitialized constant ActiveRecord (NameError)

I am running a script (that a friend I lost touch with wrote for me.) It starts like this:

require 'ruby-debug'
require 'circle'

first_circle=Circle.new()
@number_of_rounds=1

But I keep getting this error message:

C:/Ruby193/lib/ruby/gems/1.9.1/gems/circle-0.0.2/lib/circle/models/friendship.rb:1:in
`<top (required)>': uninitialized constant ActiveRecord (NameError)
  from
C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in
`require'
  from
C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in
`require'
  from
C:/Ruby193/lib/ruby/gems/1.9.1/gems/circle-0.0.2/lib/circle/circle.rb:1:in
`<top (required)>'
  from
C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in
`require'
  from
C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in
`require'
  from
C:/Ruby193/lib/ruby/gems/1.9.1/gems/circle-0.0.2/lib/circle.rb:7:in
`<top (required)>'
  from
C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in
`require'
  from
C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in
`rescue in require'
  from
C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:35:in
`require'
  from primes.rb:5:in `<main>'

What should I do?

like image 304
Jonathan Kav Avatar asked Mar 31 '13 15:03

Jonathan Kav


1 Answers

This was cross-posted to ruby-talk.

ActiveRecord is a class that talks to databases, this gem is expecting to be run in a context with a database connection to ActiveRecord loaded. If you're in Rails, that means loading your Rails environment. Or if just ActiveRecord, something like this will work:

require 'active_record'
require 'circle'

ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
ActiveRecord::Schema.define do
  self.verbose = false

  create_table :users do |t|
    t.string  :name
    t.integer :friends_count, :default => 0, :null => false
  end

  create_table :friendships, :force => true do |t|
    t.references :user, :friend
    t.datetime :requested_at, :accepted_at, :denied_at, :blocked_at
    t.string :status
    t.timestamps
  end

  create_table :blocked_users, :force => true do |t|
    t.references :user, :blocked_user
    t.timestamps
  end

  change_table :friendships do |t|
    t.index :user_id
    t.index :friend_id
    t.index :status
  end

  change_table :blocked_users do |t|
    t.index :user_id
    t.index :blocked_user_id
  end
end

class User < ActiveRecord::Base
  has_circle
end

john = User.create! name: 'john'
mary = User.create! name: 'mary'
paul = User.create! name: 'paul'

john.befriend(mary)
john.friends?(mary)               # => false
mary.accept_friend_request(john)
mary.friends?(john)               # => true

But to be honest, if you don't know what ActiveRecord is, then it seems improbable that this gem will be solving problems for you. Also, I'd be a bit skeptical of this gem, it has a misspelling in its migration such that it doesn't actually work unless you go fix it. This has been broken for at least 7 months without being fixed. There's < 800 downloads of the gem, which is not many (few users = fewer people finding and fixing bugs), and it doesn't look like the author is intending to maintain it.


Okay, I just realized what is actually happening. Took about 20 min to write that up above, and it might help someone later googling for an issue, so I'm going to leave it. What really happened, I suspect, is that you have a gem on your system named circle, and you have a file probably in your same directory named circle. Your load path is not properly set, so when you require 'circle', it is finding the gem and not the file you wrote. A simple answer is to say require File.dirname(__FILE__) + '/circle' instead of require 'circle' This isn't really the right answer, but it will work without going into the myriad of nuances required to figure out what the right thing is. If you want to figure out what the right thing is, I'd need to know what Ruby version you're on, how you're intending to use and invoke this code, and what your directory structure looks like.

Also. If you would have said that circle.rb was a file in the same directory, then I wouldn't have lost 20 min with the top answer. You should provide sufficient context in the future to understand the problem.

like image 129
Joshua Cheek Avatar answered Sep 29 '22 12:09

Joshua Cheek