title: Model is invalid - probably something's wrong with association
body: <p>I want to create app for my own usage.
  I want to encourage my kids to eat more vegetables and fruits by creating a game. Game should have players, snacks and date. Every time, when game is created it should randomly assign selected in game snacks to selected in game players. When player eat randomly assigned snack, they gain points assigned to each snack and when player collect x points, they can exchange it for award. Each player has many games and could have many snacks in each game and each snack could belong to many games and to many players in each game.</p>
  <p>I'm self learning beginner at rails, so if you have idea's how to modify it all, I'll be thankful for suggestions.</p>
  <p>After few suggestions by random people, I got implement it all as below.
  So I'm stack at creating game, because I don't know how to after creating the game assign selected snacks to players because their id's are not saved and I don't know how to modify it. Probably I could change association's to be a mirror's like players associaton's but I'm not quite sure is it a good way.</p>
  <pre><code>Processing by GamesController#create as TURBO_STREAM
    Parameters: {&quot;authenticity_token&quot;=&gt;&quot;[FILTERED]&quot;, &quot;game&quot;=&gt;{&quot;game_date(1i)&quot;=&gt;&quot;2022&quot;, &quot;game_date(2i)&quot;=&gt;&quot;5&quot;, &quot;game_date(3i)&quot;=&gt;&quot;18&quot;, &quot;player_ids&quot;=&gt;[&quot;1&quot;], &quot;snack_ids&quot;=&gt;[&quot;1&quot;]}, &quot;commit&quot;=&gt;&quot;Create Game&quot;}
  Unpermitted parameter: :snack_ids. Context: { controller: GamesController, action: create, request: #&lt;ActionDispatch::Request:0x00007fad6c1eed50&gt;, params: {&quot;authenticity_token&quot;=&gt;&quot;[FILTERED]&quot;, &quot;game&quot;=&gt;{&quot;game_date(1i)&quot;=&gt;&quot;2022&quot;, &quot;game_date(2i)&quot;=&gt;&quot;5&quot;, &quot;game_date(3i)&quot;=&gt;&quot;18&quot;, &quot;player_ids&quot;=&gt;[&quot;1&quot;], &quot;snack_ids&quot;=&gt;[&quot;1&quot;]}, &quot;commit&quot;=&gt;&quot;Create Game&quot;, &quot;controller&quot;=&gt;&quot;games&quot;, &quot;action&quot;=&gt;&quot;create&quot;} }
    Player Load (0.6ms)  SELECT &quot;players&quot;.* FROM &quot;players&quot; WHERE &quot;players&quot;.&quot;id&quot; = ?  [[&quot;id&quot;, 1]]
    ↳ app/controllers/games_controller.rb:27:in `create'
    TRANSACTION (0.1ms)  begin transaction
    ↳ app/controllers/games_controller.rb:30:in `block in create'
    Player Exists? (0.5ms)  SELECT 1 AS one FROM &quot;players&quot; WHERE &quot;players&quot;.&quot;name&quot; = ? AND &quot;players&quot;.&quot;id&quot; != ? LIMIT ?  [[&quot;name&quot;, &quot;Tata&quot;], [&quot;id&quot;, 1], [&quot;LIMIT&quot;, 1]]
    ↳ app/controllers/games_controller.rb:30:in `block in create'
    Game Create (1.4ms)  INSERT INTO &quot;games&quot; (&quot;game_date&quot;, &quot;created_at&quot;, &quot;updated_at&quot;) VALUES (?, ?, ?)  [[&quot;game_date&quot;, &quot;2022-05-18&quot;], [&quot;created_at&quot;, &quot;2022-05-18 11:04:41.815456&quot;], [&quot;updated_at&quot;, &quot;2022-05-18 11:04:41.815456&quot;]]
    ↳ app/controllers/games_controller.rb:30:in `block in create'
    PlayerGame Create (0.9ms)  INSERT INTO &quot;player_games&quot; (&quot;player_id&quot;, &quot;game_id&quot;, &quot;created_at&quot;, &quot;updated_at&quot;) VALUES (?, ?, ?, ?)  [[&quot;player_id&quot;, 1], [&quot;game_id&quot;, 10], [&quot;created_at&quot;, &quot;2022-05-18 11:04:41.820295&quot;], [&quot;updated_at&quot;, &quot;2022-05-18 11:04:41.820295&quot;]]
    ↳ app/controllers/games_controller.rb:30:in `block in create'
    TRANSACTION (8.8ms)  commit transaction
    ↳ app/controllers/games_controller.rb:30:in `block in create'
  Redirected to http://localhost:3000/games/10
  Completed 302 Found in 38ms (ActiveRecord: 13.3ms | Allocations: 9505)
  
  
  Started GET &quot;/games/10&quot; for 127.0.0.1 at 2022-05-18 13:04:41 +0200
  Processing by GamesController#show as TURBO_STREAM
    Parameters: {&quot;id&quot;=&gt;&quot;10&quot;}
    Game Load (0.6ms)  SELECT &quot;games&quot;.* FROM &quot;games&quot; WHERE &quot;games&quot;.&quot;id&quot; = ? LIMIT ?  [[&quot;id&quot;, 10], [&quot;LIMIT&quot;, 1]]
    ↳ app/controllers/games_controller.rb:66:in `set_game'
    Rendering layout layouts/application.html.erb
    Rendering games/show.html.erb within layouts/application
    Rendered games/_game.html.erb (Duration: 1.3ms | Allocations: 222)
    Rendered games/show.html.erb within layouts/application (Duration: 13.1ms | Allocations: 1433)
    Rendered layout layouts/application.html.erb (Duration: 36.0ms | Allocations: 3738)
  Completed 200 OK in 53ms (Views: 40.6ms | ActiveRecord: 0.6ms | Allocations: 5405)
  
  
  
  </code></pre>
  <p>Models associations looks like that :</p>
  <pre><code>class Game &lt; ApplicationRecord
  
    validates :game_date, comparison: { greater_than_or_equal_to: Date.today }
  
    # after_create :assign_snacks_to_players
  
    has_many :player_games
    has_many :players, through: :player_games
  
    has_many :snack_games
    has_many :snacks, through: :snack_games
  
    accepts_nested_attributes_for :snacks
  
  end
  
  
  
  class Player &lt; ApplicationRecord
    validates :name, length: {minimum: 3, maximum: 20}, uniqueness: true
    validates :points, presence: true
    validates :active, inclusion: { in: [true, false] }
  
    has_many :player_games
    has_many :games, through: :player_games
  
    has_many :snack_games
    has_many :snacks, through: :snack_games
  
  end
  
  
  class Snack &lt; ApplicationRecord
    validates :name, length: {minimum: 3, maximum: 20}, uniqueness: true
    validates :points, presence: true
    validates :active, inclusion: { in: [true, false] }
  
    has_many :snack_games
    has_many :games, through: :snack_games
  end
  
  
  class PlayerGame &lt; ApplicationRecord
    belongs_to :game
    belongs_to :player
  end
  
  
  class SnackGame &lt; ApplicationRecord
    belongs_to :snack
    belongs_to :game
    belongs_to :player
  end
  
  </code></pre>
  <p>Schema :</p>
  <pre><code># This file is auto-generated from the current state of the database. Instead
  # of editing this file, please use the migrations feature of Active Record to
  # incrementally modify your database, and then regenerate this schema definition.
  #
  # This file is the source Rails uses to define your schema when running `bin/rails
  # db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
  # be faster and is potentially less error prone than running all of your
  # migrations from scratch. Old migrations may fail to apply correctly if those
  # migrations use external dependencies or application code.
  #
  # It's strongly recommended that you check this file into your version control system.
  
  ActiveRecord::Schema[7.0].define(version: 2022_05_11_213227) do
    create_table &quot;games&quot;, force: :cascade do |t|
      t.date &quot;game_date&quot;, default: &quot;2022-05-17&quot;, null: false
      t.datetime &quot;created_at&quot;, null: false
      t.datetime &quot;updated_at&quot;, null: false
    end
  
    create_table &quot;player_games&quot;, force: :cascade do |t|
      t.integer &quot;player_id&quot;, null: false
      t.integer &quot;game_id&quot;, null: false
      t.datetime &quot;created_at&quot;, null: false
      t.datetime &quot;updated_at&quot;, null: false
      t.index [&quot;game_id&quot;], name: &quot;index_player_games_on_game_id&quot;
      t.index [&quot;player_id&quot;], name: &quot;index_player_games_on_player_id&quot;
    end
  
    create_table &quot;players&quot;, force: :cascade do |t|
      t.string &quot;name&quot;, null: false
      t.integer &quot;points&quot;, default: 0, null: false
      t.boolean &quot;active&quot;, default: true, null: false
      t.datetime &quot;created_at&quot;, null: false
      t.datetime &quot;updated_at&quot;, null: false
    end
  
    create_table &quot;snack_games&quot;, force: :cascade do |t|
      t.integer &quot;snack_id&quot;, null: false
      t.integer &quot;game_id&quot;, null: false
      t.datetime &quot;created_at&quot;, null: false
      t.datetime &quot;updated_at&quot;, null: false
      t.integer &quot;player_id&quot;, null: false
      t.index [&quot;game_id&quot;], name: &quot;index_snack_games_on_game_id&quot;
      t.index [&quot;player_id&quot;], name: &quot;index_snack_games_on_player_id&quot;
      t.index [&quot;snack_id&quot;], name: &quot;index_snack_games_on_snack_id&quot;
    end
  
    create_table &quot;snacks&quot;, force: :cascade do |t|
      t.string &quot;name&quot;, null: false
      t.integer &quot;points&quot;, default: 0, null: false
      t.boolean &quot;active&quot;, default: true, null: false
      t.datetime &quot;created_at&quot;, null: false
      t.datetime &quot;updated_at&quot;, null: false
    end
  
    add_foreign_key &quot;player_games&quot;, &quot;games&quot;
    add_foreign_key &quot;player_games&quot;, &quot;players&quot;
    add_foreign_key &quot;snack_games&quot;, &quot;games&quot;
    add_foreign_key &quot;snack_games&quot;, &quot;players&quot;
    add_foreign_key &quot;snack_games&quot;, &quot;snacks&quot;
  end
  
  </code></pre>
  <p>I think player and snack controller's are not necessary so I include only games_controller</p>
  <pre><code>class GamesController &lt; ApplicationController
    before_action :set_game, only: %i[ show edit update destroy ]
  
  
    # Find solution to assign players and snacks after create game not only from view but also from terminal
  
    # GET /games or /games.json
    def index
      @games = Game.all
    end
  
    # GET /games/1 or /games/1.json
    def show
    end
  
    # GET /games/new
    def new
      @game = Game.new
    end
  
    # GET /games/1/edit
    def edit
    end
  
    # POST /games or /games.json
    def create
      @game = Game.new(game_params)
  
      respond_to do |format|
        if @game.save
          format.html { redirect_to game_url(@game), notice: &quot;Game was successfully created.&quot; }
          format.json { render :show, status: :created, location: @game }
        else
          format.html { render :new, status: :unprocessable_entity }
          format.json { render json: @game.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # PATCH/PUT /games/1 or /games/1.json
    def update
      respond_to do |format|
        if @game.update(game_params)
          format.html { redirect_to game_url(@game), notice: &quot;Game was successfully updated.&quot; }
          format.json { render :show, status: :ok, location: @game }
        else
          format.html { render :edit, status: :unprocessable_entity }
          format.json { render json: @game.errors, status: :unprocessable_entity }
        end
      end
    end
  
    # DELETE /games/1 or /games/1.json
    def destroy
      @game.destroy
  
      respond_to do |format|
        format.html { redirect_to games_url, notice: &quot;Game was successfully destroyed.&quot; }
        format.json { head :no_content }
      end
    end
  
    private
      # Use callbacks to share common setup or constraints between actions.
      def set_game
        @game = Game.find(params[:id])
      end
  
      # Only allow a list of trusted parameters through.
      def game_params
        params.require(:game).permit(:game_date, player_ids: [], player_attributes: [:name])
      end
  
  end
  </code></pre>
  <p>and here is a form, but not correct with :snack_ids</p>
  <pre><code>&lt;%= form_with(model: game) do |form| %&gt;
    &lt;% if game.errors.any? %&gt;
      &lt;div style=&quot;color: red&quot;&gt;
        &lt;h2&gt;&lt;%= pluralize(game.errors.count, &quot;error&quot;) %&gt; prohibited this game from being saved:&lt;/h2&gt;
  
        &lt;ul&gt;
          &lt;% game.errors.each do |error| %&gt;
            &lt;li&gt;&lt;%= error.full_message %&gt;&lt;/li&gt;
          &lt;% end %&gt;
        &lt;/ul&gt;
      &lt;/div&gt;
    &lt;% end %&gt;
  
    &lt;div&gt;
      &lt;%= form.label :game_date, style: &quot;display: block&quot; %&gt;
      &lt;%= form.date_select :game_date %&gt;
    &lt;/div&gt;
  
    &lt;div&gt;
      &lt;%= form.collection_check_boxes :player_ids, Player.where(active: true), :id, :name, include_hidden: false %&gt;
    &lt;/div&gt;
  
  
    &lt;div&gt;
      &lt;%= form.collection_check_boxes :snack_ids, Snack.where(active: true), :id, :name, include_hidden: false %&gt;
    &lt;/div&gt;
  
  
    
    &lt;div&gt;
      &lt;%= form.submit %&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  <p>I know I could associate snack with player by creating in IRB for game SnackGame with player id and snack id but I dont know how to handle it in form and model</p>