Uninitialized Constant Applicationrecord Error

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



Leave a reply



Submit