uninitialized constant ApplicationRecord
Asked Answered
A

3

20

I am working on the rails tutorial book online and I am getting the following error message when I go to http://localhost:3000/

"uninitialized constant ApplicationRecord"

and it gives me the following code highlighting the first line.

class User < ApplicationRecord
  attr_accessor :remember_token
  before_save { self.email = email.downcase }
  validates :name,  presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, length: { maximum: 255 },

Here is my application.html.erb file:

<!DOCTYPE html>
<html>
  <head>
    <title><%= full_title(yield(:title)) %></title>
    <%= stylesheet_link_tag "application", media: "all",
                                           "data-turbolinks-track" => true %>
    <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
    <%= csrf_meta_tags %>
    <%= render 'layouts/shim' %>
  </head>
  <body>
    <%= render 'layouts/header' %>
    <div class="container">
      <% flash.each do |message_type, message| %>
        <div class="alert alert-<%= message_type %>"><%= message %></div>
      <% end %>
      <%= yield %>
      <%= render 'layouts/footer' %>
      <%= debug(params) if Rails.env.development? %>
    </div>
  </body>
</html>

And my user.rb file :

class User < ApplicationRecord
  attr_accessor :remember_token
  before_save { self.email = email.downcase }
  validates :name,  presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, length: { maximum: 255 },
                    format: { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
  has_secure_password
  validates :password, presence: true, length: { minimum: 6 }

  # Returns the hash digest of the given string.
  def User.digest(string)
    cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
                                                  BCrypt::Engine.cost
    BCrypt::Password.create(string, cost: cost)
  end

  # Returns a random token.
  def User.new_token
    SecureRandom.urlsafe_base64
  end

  # Remembers a user in the database for use in persistent sessions.
  def remember
    self.remember_token = User.new_token
    update_attribute(:remember_digest, User.digest(remember_token))
  end

  # Returns true if the given token matches the digest.
  def authenticated?(remember_token)
    return false if remember_digest.nil?
    BCrypt::Password.new(remember_digest).is_password?(remember_token)
  end

  # Forgets a user.
  def forget
    update_attribute(:remember_digest, nil)
  end
end
Alburnum answered 12/7, 2016 at 1:28 Comment(5)
could you check the presence of app/models/application_record.rb?Pinstripe
I don't have an application_record.rb file, should I have one?Alburnum
Application Record is only for rails 5. Are you running rals 5?Atomize
No, im not running rails 5, im runing rails 4.Alburnum
that was the problem I was running rails 4Alburnum
P
32

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

Pinstripe answered 12/7, 2016 at 1:53 Comment(0)
C
25

Referring to infused's answer from https://mcmap.net/q/661829/-uninitialized-constant-applicationrecord-error

Create a new file called app/models/application_record.rb with the following content:

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
end
Chemesh answered 5/8, 2017 at 21:54 Comment(0)
B
6

If you are getting this after moving from Rails 5 to Rails 6, ensure you change

config.load_defaults 5.2

for

config.load_defaults 6.0

in your config/application.rb file.

Butcher answered 9/9, 2019 at 15:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.