uninitialized constant ApplicationRecord
It appears you're using the Rails 5 tutorial, but working with Rails 4. In Rails 5 all models inherit from ApplicationRecord
, while Rails 4 from ActiveRecord::Base
Immediate fix:
class User < ActiveRecord::Base
...
end
Long term fix, switch to Rails 5 and learn with Rails 5
uninitialized constant ApplicationRecord error
Create a new file called app/models/application_record.rb with the following contents:
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
Explanation: When generating a new Rails 5+ project the application_record.rb file will be created automatically, but if using a project that was generated on an earlier version and upgraded to 5+ you need to create this file yourself.
uninitialized constant ApplicationRecord rspec
For rails specs use require 'rails-helper'
at beginning of each spec file (it is generated by bin/rails generate rspec:install
from rspec-rails
gem)
It contains line require File.expand_path('../config/environment', __dir__)
that will load your rails environment and you'll have autoloading and all other rails parts working.
Ruby on rails 5 uninitialized constant ApplicationRecord (NameError)
Make sure that the app/models/application_record.rb
file exists and that it has the following (or similar) code
# Base ApplicationRecord Class
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
If not, add it. It should have been generated by rails new
.
NameError (uninitialized constant ApplicationRecord) in has_many through association
Looks like you do not have an ApplicationRecord
model (you do not have to be on Rails 5+ to do that, actually it's a good idea to adopt this prior to update):
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
uninitialized constant ApplicationRecord caused by factory girl?
Since Rails 5 a model file application_record.rb
with class ApplicationRecord
is generated. Every new model should inherit from that class instead of ActiveRecord::Base
to prevent mokey patching of ActiveRecord::Base
. ApplicationRecord
is the new entry point for extensions.
Related Topics
Idiomatically Mock Openuri.Open_Uri with Minitest
How to Upgrade Rvm When the Official Way Doesn't Work
Static Variables in Ruby, Like in C Functions
Convert String to Datetime Ruby on Rails
Accessing a Value in a Method Using *
Heroku: Error Pushing Rails App to Heroku, Heroku Can't Find Rails App
Why Doesn't Puts() Print in a Single Line
Devise Rendering Default Views from Gem Instead of Generated Ones
Exclude Some Ids from Result in Rails Activerecord
Convert String Numbers( in Word Format) to Integer Ruby
Openuri Causing 401 Unauthorized Error with Https Url
Regex to Match Mm/Dd/Yyyy Hh:Mm:Ss Am or Pm
Rails 3.2 Force_Ssl Except on Landing Page
Ruby Parenthesis Syntax Exception with I++ ++I
Incompatible Character Encoding in Rails - How to Just Fail/Skip Sensibly