0
votes

I am getting this error: NameError in ScoersController#create uninitialized constant Score::UserTo Does anyone know what could be causing this error? I don't even know what uninitialized conatant this could be referring to.

Here is my ScoresController

class ScoresController < ApplicationController
  before_action :set_score, only: [:show, :edit, :update, :destroy]

  # GET /scores
  # GET /scores.json
  def index
    @scores = Score.all
  end

  # GET /scores/1
  # GET /scores/1.json
  def show
  end

  # GET /scores/new
  def new
    @score = Score.new(project_id: params[:project_id])
    @usersFor = User.all
    @left = User.all
  end

  # GET /scores/1/edit
  def edit
  end

  # POST /scores
  # POST /scores.json
  def create
    @usersFor = User.all
    @left = User.all
    @score = Score.new(score_params)

    respond_to do |format|
      if @score.save
        format.html { redirect_to @score, notice: 'Score was successfully created.' }
        format.json { render :show, status: :created, location: @score }
      else
        format.html { render :new }
        format.json { render json: @score.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /scores/1
  # PATCH/PUT /scores/1.json
  def update
    respond_to do |format|
      if @score.update(score_params)
        format.html { redirect_to @score, notice: 'Score was successfully updated.' }
        format.json { render :show, status: :ok, location: @score }
      else
        format.html { render :edit }
        format.json { render json: @score.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /scores/1
  # DELETE /scores/1.json
  def destroy
    @score.destroy
    respond_to do |format|
      format.html { redirect_to scores_url, notice: 'Score was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_score
      @score = Score.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def score_params
      params.require(:score).permit(:project_id, :user_to_id, :user_for_id, :score)
    end
end

Here is my scores/new.html.erb file:

<% @project = Project.find_by id: params[:project_id] %>
<h1>New Score</h1>

<%= form_with(model: @score, local: true) do |form| %>
  <% if @score.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@score.errors.count, "error") %> prohibited this score from being saved:</h2>

      <ul>
        <% @score.errors.full_messages.each do |message| %>
          <li><%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>
  <% @projects = Project.where(id: params[:project_id]) %>
  <% @usersFor = User.where(id: current_user.id) %>
  <% @given = Score.where(project_id: @project.id, user_for_id: current_user.id) %>
  <% @evaluated = [] %>
  <% @given.each do |gave| %>
    <% @teammate = User.find_by id: gave.user_to_id %>
    <% @evaluated.push(@teammate) %>
  <% end %>
  <% @team %>
  <% @teams = Team.where(team_type_id: @project.team_type_id) %>
  <% @userTeams = TeamMembership.where(user_id: current_user.id) %>
  <% @userTeams.each do |t| %>
   <% @tempTeam = Team.find_by id: t.team_id%>
    <% if @tempTeam.team_type_id == @project.team_type_id %>
      <% @team = @tempTeam %>
      <% end %>
    <% end %>
  <% @group = TeamMembership.where(team_id: @team.id) %>
  <% @left = [] %>
  <% @group.each do |g| %>
    <% @m = User.find_by id: g.user_id %>
    <% @left.push(@m) %>
  <% end %>
  <% @left = @left - @evaluated %>

  <div class="field">
    <%= form.label :project_id %>
    <%= collection_select(:score, :project_id, @projects,:id,:project_name) %>
  </div>

  <div class="field">
    <%= form.select :user_for_id, options_for_select(@usersFor.collect{ |user| [user.first_name + " "+ user.last_name, user.id]}) %>
  </div>

  <div class="field">
    <%= form.select :user_to_id, options_for_select(@left.collect{ |user| [user.first_name + " "+ user.last_name, user.id]}) %>
  </div>

  <div class="field">
    <%= form.label :score %>
    <%= form.number_field :score, id: :score_score %>
  </div>

  <div class="actions">
    <%= form.submit %>
  </div>

  <% end %>

Here is my score model if that helps:

class Score < ApplicationRecord
  validates :score, numericality: { less_than_or_equal_to: 10,
                                    only_integer: true}, presence:
                true
  belongs_to :project
  belongs_to :user_to
  belongs_to :user_for
end

Thanks! Edit: new score model

class Score < ApplicationRecord
  validates :score, numericality: { less_than_or_equal_to: 10,
                                    only_integer: true}, presence:
                true
  belongs_to :project
  has_one :user_to, class_name: User
  has_one :user_for, class_name: User
end

Edit: full error message:

ActiveRecord::StatementInvalid in ScoresController#create SQLite3::SQLException: no such table: main.user_fors: INSERT INTO "scores" ("project_id", "user_to_id", "user_for_id", "score", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?) The error is raised on if @score.save line in ScoresController

1
whats on your User model? - Abdul Wahed

1 Answers

0
votes

Your associations show user_to, rails is going to try and find a class called UserTo unless you specify otherwise. You can pass class_name: User to the association to tell it which class to use.