开发者

Saving model associations in the controller

开发者 https://www.devze.com 2023-01-18 23:50 出处:网络
I have the following models set up: class Player < ActiveRecord::Base has_many :game_players has_many :games, :through => :game_players

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 i开发者_运维技巧s 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 %>


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

精彩评论

暂无评论...
验证码 换一张
取 消