Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3220408
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:45:29+00:00 2026-05-17T15:45:29+00:00

I have the following models set up: class Player < ActiveRecord::Base has_many :game_players has_many

  • 0

I have the following models set up:

class Player < ActiveRecord::Base
  has_many :game_players
  has_many :games, :through => :game_players
end

class Game < ActiveRecord::Base
  has_many :game_players
  has_many :players, :through => :game_players 
end

class GamePlayer < ActiveRecord::Base
  belongs_to :game
  belongs_to :player
end

Each Game has four players. I need some guidance in the controller for what saving a new game and it’s 4 players in the GamePlayer model would look like. This is what I have in my controller so far:

  # POST /games
  # POST /games.xml
  def create
    @game = Game.new(params[:game])

    respond_to do |format|
      if @game.save

        format.html { redirect_to(@game, :notice => 'Game was successfully created.') }
        format.xml  { render :xml => @game, :status => :created, :location => @game }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @game.errors, :status => :unprocessable_entity }
      end
    end
  end

Obviously it’s only saving the Game model but it is unclear to me how to go about creating and saving the 4 players in GamePlayer. GamePlayer is really just supposed to have a game_id and player_id with some extra meta data for each row (like who played defense, etc.).

Any help would be appreciated.

In my view form I have the following:

<%= form_for(@game) do |f| %>
  <% if @game.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@game.errors.count, "error") %> prohibited this game from being saved:</h2>

      <ul>
      <% @game.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :blue_score %> :
    <%= f.text_field :blue_score %><br />
    <%= f.label :white_score %> :
    <%= f.text_field :white_score %><br />


    <%= label_tag(:white_offense, "White Offense:") %>
    <%= select_tag "white_offense", options_from_collection_for_select(@players, "name", "id")
    <%= select_tag "white_defense", options_from_collection_for_select(@players, "name", "id")
    <%= select_tag "blue_offense", options_from_collection_for_select(@players, "name", "id")
    <%= select_tag "blue_defense", options_from_collection_for_select(@players, "name", "id")


    <%= submit_tag("New Game") %>


  </div>
<% end %>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:45:29+00:00Added an answer on May 17, 2026 at 3:45 pm

    Although I feel like there has to be a better way than this (perhaps using Collections?), here are is my solution:

    <%= label_tag(:white_offense, "White Offense:") %>
    <%= select_tag "white_offense", options_from_collection_for_select(@players, "id", "name") %>
    <br />
    <%= label_tag(:white_defense, "White Defense:") %>
    <%= select_tag "white_defense", options_from_collection_for_select(@players, "id", "name") %>
    <br />
    <%= label_tag(:blue_offense, "Blue Offense:") %>
    <%= select_tag "blue_offense", options_from_collection_for_select(@players, "id", "name") %>
    <br />
    <%= label_tag(:blue_defense, "Blue Defense:") %>
    <%= select_tag "blue_defense", options_from_collection_for_select(@players, "id", "name") %>
    <br />
    <%= submit_tag("New Game") %>
    

    And my controller:

    @game = Game.new(params[:game])
    @game.game_players.build(:player_id => params[:white_offense])
    @game.game_players.build(:player_id => params[:white_defense], :is_defender => 1)
    @game.game_players.build(:player_id => params[:blue_offense], :is_blue => 1)
    @game.game_players.build(:player_id => params[:blue_defense], :is_blue => 1, :is_defender => 1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following models: class User < ActiveRecord::Base has_many :subscriptions end class Subscription
I have the following models set up: class Contact < ActiveRecord::Base belongs_to :band belongs_to
I have the following models: class Aircon < ActiveRecord::Base belongs_to :shop belongs_to :brand belongs_to
I have the following models: class Aircon < ActiveRecord::Base belongs_to :shop belongs_to :brand belongs_to
I have the following models: class Product < ActiveRecord::Base belongs_to :brand belongs_to :model accepts_nested_attributes_for
I have the following model set up: class UserProfile(models.Model): Additional attributes for users. url
I have the following two models class Author(Model): name = CharField() class Publication(Model): title
I have the following in my models.py: class HostData(models.Model): Manager = models.ForeignKey(Managers) Host =
I have the following model: from django.db import models class State(models.Model): name = models.CharField(max_length=30)
I have the following model: public class ABaseObject { private Guid id = Guid.NewGuid();

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.