Shane
Shane

Reputation: 5667

uninitialized constant ActiveRecord

2.0.0-p247 :006 > load './app/models/user.rb'                                                                                                                                                     
NameError: uninitialized constant ActiveRecord                                                                                                                                                    
        from /home/action/iAuth/app/models/user.rb:1:in `<top (required)>'                                                                                                                        
        from (irb):6:in `load'                                                                                                                                                                    
        from (irb):6                                                                                                                                                                              
        from /home/action/.rvm/rubies/ruby-2.0.0-p247/bin/irb:13:in `<main>'   

Below is my User Model.

class User < ActiveRecord::Base
end

When i try to load my user.rb in my irb, i get the above error.

Upvotes: 2

Views: 4841

Answers (3)

stevec
stevec

Reputation: 52218

I got this error when trying to run test in a new ruby gem I made.

I added this to the top of spec/spec_helper.rb

require "active_record/railtie"

and the problem went away.

But a better way to solve it (I think) is to require the library in gemspec:

  # Uncomment to register a new dependency of your gem
  # spec.add_dependency "example-gem", "~> 1.0"
  spec.add_dependency "rails" # <----- Add this line

Upvotes: 0

Siwei
Siwei

Reputation: 21549

there's another case.

if you are using mongoid ( mongo db adapter) , and you have to make sure in config/application.rb, you must require activerecord:

require "active_model/railtie"

Upvotes: 0

bjhaid
bjhaid

Reputation: 9752

start your irb session with

rails console

and not:

irb

rails console would load your rails environment and your model for you, so you can do things like:

User.all or User.new without loading the class as it has been preloaded by rails console already

Upvotes: 8

Related Questions