NameError uninitialized constant Model::Object

18.4k views Asked by At

I'm new to ruby on rails. Ihe error I have is

NameError in ReviewsController#create
uninitialized constant User::Review
Extracted source:
    @review = current_user.reviews.build(review_params)

I read on other stack overflow questions that usually the error for wrong names or forgetting belongs_to or has_many but I believe I've set the relations correctly. I am using the gem devise to handle the user and sign in/sign up etc

Reviews.rb

class Reviews < ActiveRecord::Base

  belongs_to :user
    belongs_to :renters

end

User.rb

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :reviews
end

Reviews_Controller.rb

class ReviewsController < ApplicationController
  before_action :set_renter
  before_action :authenticate_user!

  def new
    @review = Reviews.new(renters: @renter)
  end

  def create
    @review = current_user.reviews.build(review_params)
    @review.renter = @renter
    @review.save 
    redirect_to @renter 
  end

  private
    def set_renter
      @renter = Renters.find(params[:renter_id])
    end

    def review_params
      params.require(:reviews).permit(:comment, :rating)
    end
end

The Renters model is working fine and similar code I have to make a new Renter is working so I am not sure what is wrong.

3

There are 3 answers

1
spickermann On BEST ANSWER

ActiveRecord::Base classes are usually named in singular.

Ruby on Rails naming conventions expect your class to be named Review and it should be stored in a file named models/review.rb (but still store its entries in a reviews database table).

If you do not want to follow this convention, then you have to explicitly tell Rails that the class is named differently in the definition of the belongs_to and has_many association.

Because such conventions make it much easier to work with Ruby on Rails, I highly recommend following and not fighting those naming conventions.

0
sa77 On

your model class for your reviews table should be Review in the file: app/models/review.rb

class Review < ActiveRecord::Base
  belongs_to :user
  belongs_to :renters
end

and your User model representing users table should be in the file: app/models/user.rb

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :reviews
end

NOTE: for this association to work, your reviews table must have a column user_id as the foreign key for performing activerecord operations on associated models (for example: User.find(1).reviews to get all records of reviews table whose user_id is 1)

0
Darlan Dieterich On

This occours by convention of Rails. You can force with function class_name the class Reviews

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :reviews, class_name: "Reviews"
end