title: Foreign keys working for one table but not another?
body: <p>I have two different tables, a <code>Team</code> table and a <code>Project</code> table. Each table has a foreign key to the <code>TeamType</code> table. I'm using a form to populate each table that uses <code>collection_select</code> to choose the team type to have a foreign key to. This works perfectly for the <code>Team</code> table but when I try to fill out the form for the <code>Project</code> table, it gives me an error saying "Team type must exist", even though the models and migrations are structured almost identically. What could be the issue here?
  Here are my migrations:</p>
  
  <pre><code>class CreateProjects &lt; ActiveRecord::Migration[5.1]
    def change
      create_table :projects do |t|
        t.references :team_type, foreign_key: true
        t.string :project_name
  
        t.timestamps
      end
    end
  end
  </code></pre>
  
  <pre><code>class CreateTeams &lt; ActiveRecord::Migration[5.1]
    def change
      create_table :teams do |t|
        t.references :team_type, foreign_key: true
        t.string :team_name
        t.integer :num_of_students
  
        t.timestamps
      end
    end
  end
  </code></pre>
  
  <p>Here are my models:</p>
  
  <pre><code>class Project &lt; ApplicationRecord
    validates :project_name, length: {maximum: 35}, presence: true
    belongs_to :team_type
    # add_index :team
  end
  </code></pre>
  
  <pre><code>class Team &lt; ApplicationRecord
    validates :team_name, length: {maximum: 40}, presence: true
    belongs_to :team_type
    # add_index :team_name
  end
  </code></pre>
  
  <p>Here is the HTML of the forms for <code>Project</code> and <code>Team</code> respectively:</p>
  
  <pre><code>  &lt;div class="field"&gt;
      &lt;%= form.label :team_type_id %&gt;
      &lt;%= collection_select(:project, :team_type_id, TeamType.all,:id,:type_name) %&gt;
    &lt;/div&gt;
  </code></pre>
  
  <pre><code>  &lt;div class="field"&gt;
      &lt;%= form.label :team_type_id %&gt;
      &lt;%= collection_select(:team, :team_type_id, TeamType.all,:id,:type_name) %&gt;
    &lt;/div&gt;
  </code></pre>
  
  <p>Here are the controllers:</p>
  
  <pre><code>class ProjectsController &lt; ApplicationController
    before_action :set_project, only: [:show, :edit, :update, :destroy]
  
    # GET /projects
    # GET /projects.json
    def index
      @projects = Project.all
    end
  
    # GET /projects/1
    # GET /projects/1.json
    def show
    end
  
    # GET /projects/new
    def new
      @project = Project.new
    end
  
    # GET /projects/1/edit
    def edit
    end
  
    # POST /projects
    # POST /projects.json
    def create
      @project = Project.new(project_params)
  
      respond_to do |format|
        if @project.save
          format.html { redirect_to @project, notice: 'Project was successfully created.' }
          format.json { render :show, status: :created, location: @project }
        else
          format.html { render :new }
          format.json { render json: @project.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # PATCH/PUT /projects/1
    # PATCH/PUT /projects/1.json
    def update
      respond_to do |format|
        if @project.update(project_params)
          format.html { redirect_to @project, notice: 'Project was successfully updated.' }
          format.json { render :show, status: :ok, location: @project }
        else
          format.html { render :edit }
          format.json { render json: @project.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # DELETE /projects/1
    # DELETE /projects/1.json
    def destroy
      @project.destroy
      respond_to do |format|
        format.html { redirect_to projects_url, notice: 'Project was successfully destroyed.' }
        format.json { head :no_content }
      end
    end
  
    private
      # Use callbacks to share common setup or constraints between actions.
      def set_project
        @project = Project.find(params[:id])
      end
  
      # Never trust parameters from the scary internet, only allow the white list through.
      def project_params
        params.require(:project).permit(:project_name, :team_id)
      end
  end
  </code></pre>
  
  <pre><code>class TeamsController &lt; ApplicationController
    before_action :set_team, only: [:show, :edit, :update, :destroy]
  
    # GET /teams
    # GET /teams.json
    def index
      @teams = Team.all
    end
  
    # GET /teams/1
    # GET /teams/1.json
    def show
    end
  
    # GET /teams/new
    def new
      @team = Team.new
    end
  
    # GET /teams/1/edit
    def edit
    end
  
    # POST /teams
    # POST /teams.json
    def create
      @team = Team.new(team_params)
  
      respond_to do |format|
        if @team.save
          format.html { redirect_to @team, notice: 'Team was successfully created.' }
          format.json { render :show, status: :created, location: @team }
        else
          format.html { render :new }
          format.json { render json: @team.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # PATCH/PUT /teams/1
    # PATCH/PUT /teams/1.json
    def update
      respond_to do |format|
        if @team.update(team_params)
          format.html { redirect_to @team, notice: 'Team was successfully updated.' }
          format.json { render :show, status: :ok, location: @team }
        else
          format.html { render :edit }
          format.json { render json: @team.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # DELETE /teams/1
    # DELETE /teams/1.json
    def destroy
      @team.destroy
      respond_to do |format|
        format.html { redirect_to teams_url, notice: 'Team was successfully destroyed.' }
        format.json { head :no_content }
      end
    end
  
    private
      # Use callbacks to share common setup or constraints between actions.
      def set_team
        @team = Team.find(params[:id])
      end
  
      # Never trust parameters from the scary internet, only allow the white list through.
      def team_params
        params.require(:team).permit(:team_type_id, :team_name, :num_of_students)
      end
  end
  </code></pre>
  
  <p>Thanks for any help, I appreciate it!</p>