'Uninitialized constant User::ChatRoom

I get this error

NameError (uninitialized constant User::ChatRoom):
app/controllers/users_controller.rb:20:in `create'

Here is my user.rb file

class User
  include Mongoid::Document
  field :name, type: String
  field :email, type: String
  has_many :chat_room, dependent: :destroy
end

Here is my chat_room.rb file

class ChatRoom
  include Mongoid::Document
  # Attributes
  field :tittle, type: String
  field :is_private, type: Boolean, default: false
  # Relations

  belongs_to :user
  # Validations and Scope
  scope :public_rooms, -> { where(is_private: false) }
end

users_controller.rb file

def create
    user = User.new(user_params)

    if user.save
      render json: { status: :created}
    else
      render json: user.errors, status: :unprocessable_entity
    end
  end



Solution 1:[1]

In your class User, change :

has_many :chat_room, dependent: :destroy

by :

has_many :chat_rooms, dependent: :destroy

After has_many, it should be the plurial class name. And after belongs_to, it should be singular class name.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Geoffrey Dulac