From: Josh Cheek Date: 2013-04-01T00:27:22+09:00 Subject: Re: uninitialized constant ActiveRecord (NameError) --e89a8f22bcc3d7a2a304d93a2382 Content-Type: text/plain; charset=ISO-8859-1 On Sun, Mar 31, 2013 at 9:32 AM, jonathan kav wrote: > Hallo > > I am running a script that 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 > `': 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 > `' > 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 > `' > 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 `
' > > Whatshould I to do? > > -- > Posted via http://www.ruby-forum.com/. > > 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. https://github.com/theymaybecoders/circle/blob/f13fa0eadc77fd97029f9983dffe499fe8e9ae39/lib/generators/circle/templates/migration.rb#L27This 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. --e89a8f22bcc3d7a2a304d93a2382 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
On Sun, Mar 31, 2013 at 9:32 AM, jonathan kav <lists@r= uby-forum.com> wrote:
Hallo

I am running a script that starts like this:

require 'ruby-debug'
require 'circle'

first_circle=3DCircle.new()
@number_of_rounds=3D1

but i keep getting this error message:

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

Whatshould I to do?

--
Posted via http://= www.ruby-forum.com/.


Activ= eRecord 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 ActiveR= ecord, something like this will work:


require 'active_record'
= require 'circle'

ActiveRecor= d::Base.establish_connection adapter: 'sqlite3', database: ':me= mory:'
ActiveRecord::Schema.define do
=A0 self.verbose =3D false
=A0=A0
=A0 create_table :users d= o |t|
=A0 =A0 t.string =A0:name
=A0 =A0 t.integer :friends_count, :default =3D> 0, = :null =3D> false
=A0 end
=A0=A0
=A0 create_table :friendships, :force =3D>= ; true do |t|
=A0 =A0 t.references :user, := friend
=A0 =A0 t.datetime :requested_at, :accepted_at, :denied_at, :blocked_at
=A0 =A0 t.string :status
=A0 =A0 t.timestamps
=A0 end

=A0 create_table :blocked_users, :forc= e =3D> true do |t|
=A0 =A0 t.references = :user, :blocked_user
=A0 =A0 t.timestamps
=A0 end

=A0 change_table :friendships do |t|
=A0 = =A0 t.index :user_id
=A0 =A0 t.index :frien= d_id
=A0 =A0 t.index :status
=A0 end

=A0 change_table = :blocked_users do |t|
=A0 =A0 t.index :user= _id
=A0 =A0 t.index :blocked_user_id
=A0 end
end

class User < ActiveRecord::Base
=A0 has_circle
end

john =3D User.create! name: &#= 39;john'
mary =3D User.create! name: &#= 39;mary'
paul =3D User.create! name: 'paul'
=

john.befrie= nd(mary)
john.friends?(mary) =A0 =A0 =A0 = =A0 =A0 =A0 =A0 # =3D> false
mary.accept_friend_request(john)
mary.friends?(john) =A0 =A0 =A0 =A0 =A0 =A0 =A0 # =3D> = true


But to be honest, if you= don't know what ActiveRecord is, then it seems improbable that this ge= m will be solving problems for you. Also, I'd be a bit skeptical of thi= s gem, it has a misspelling in its migration such that it doesn't actua= lly work unless you go fix it. https://github.com/theymaybecoders/circle/blo= b/f13fa0eadc77fd97029f9983dffe499fe8e9ae39/lib/generators/circle/templates/= migration.rb#L27 This has been broken for at least 7 months without bei= ng fixed. There's < 800 downloads of the gem, which is not many (few= users =3D fewer people finding and fixing bugs), and it doesn't look l= ike the author is intending to maintain it.

-----------------------

= Okay, I just realized what is actually happening. Took about 20 min to writ= e 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 h= ave 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 y= ou `require 'circle'`, it is finding the gem and not the file you w= rote. A simple answer is to say `require File.dirname(__FILE__) + '/cir= cle'` instead of `require 'circle'` This isn't really the r= ight answer, but it will work without going into the myriad of nuances requ= ired 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, h= ow you're intending to use and invoke this code, and what your director= y 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 wit= h the top answer. You should provide sufficient context in the future to un= derstand the problem.
--e89a8f22bcc3d7a2a304d93a2382--