Forum: Ruby on Rails Modeling/Validating a two to many Relationship

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Ffe37142b7ba8187aaa3f7fffb952d32?d=identicon&s=25 davosian (Guest)
on 2007-02-27 19:19
(Received via mailing list)
I am trying to model a soccer tournament which consists of games and
each game has two teams assigned:

The two models (Team and Game) that have a 2 : many relationship, i.e.
1 game has two teams.
I modelled this with two has_many/has_one relationships to a model
called TeamsGames. In there, I have an additional field "team_type" to
keep track of whether it is a home or a guest team. This additional
field is also the reason why I do not use a has_and_belongs_to_many

My first question is whether this design makes sense. I was thinking
of using inheritance to differenciate between home and away teams, but
because every team can be either one in different games, it does not

Anyway, I went ahead and created a form that allows you to enter game
information (e.g. the date). The same form has two drop-down menus to
choose the home and away team. The drop-downs are populated from the
teams model. This approach seems to work fine except for the fact that
I do not know how to validate the drop-down selections:

The validation needed is very simple: home and away team are required
and must be different. The problem I have is that when I use standard,
model based validation, I am checking against the database, but this
is wrong. I should only compare the two drop-down boxes as these will
update the database. I assume this has to be done in the controller

Any help would be appreciated.


Here is the code for the form, controller and my models:

[b]Edit Game Form[/b]
[code=]<h2>Edit Game Details</h2>

<% form_tag :action => 'edit_game', :id => params[:id] do %>
  <%= error_messages_for :game %>

  <p><label for="game_tournament_id">Tournament</label>
  <%= select :game, :tournament_id, @tournaments %></p>

  <p><label for="game_name">Name</label>
  <%= text_field :game, :name  %></p>

  <p><label for="game_date">Date</label>
  <%= date_select :game, :date  %></p>

  <h2>Home and Guest Teams</h2>
    <% for @teams_games in @game.teams_games %>
      <%= error_messages_for :teams_games %>

      <% fields_for 'teams_games[]' do |f| %>
        <%= :team_id, @teamslist %>
      <% end %>
    <% end %>

  <%= submit_tag 'Update' %>
  <%= link_to 'Cancel', :action => 'list_games', :id =>
@game.tournament %>
<% end %>[/code]
[b]The models[/b]
[code=]class Team < ActiveRecord::Base

# t.column :name, :string, :null => false
# t.column :founding_date, :date

  has_many :teams_games, :class_name => 'TeamsGames'
  has_many :games, :through => :teams_games

  validates_presence_of :name

class Game < ActiveRecord::Base

# t.column :name, :string
# t.column :date, :date
# t.column :tournament_id, :integer, :null => false

  belongs_to :tournament
  has_many :teams_games, :class_name => 'TeamsGames'
  has_many :teams, :through => :teams_games
  has_many :home_team,
              :through => :teams_games,
              :source => :team,
              :conditions => "teams_games.team_type = 'Home'",
              :limit => '1'
  has_many :guest_team,
              :through => :teams_games,
              :source => :team,
              :conditions => "teams_games.team_type = 'Guest'",
              :limit => '1'

  validates_presence_of :tournament_id

class TeamsGames < ActiveRecord::Base

# t.column :team_type, :string, :null => false
# t.column :team_id, :integer, :null => false
# t.column :game_id, :integer, :null => false

  belongs_to :team
  belongs_to :game

  validates_presence_of :team_id, :team_type

  def self.get_team_types
    {:home => "Home", :guest => "Guest"}
[b]The edit action[/b]
[code=]def edit_game
    @tournaments = Tournament.find(:all, :order => 'name').map {|t|
    @game = Game.find(params[:id])
    @teamslist = Team.find(:all, :order => 'name').map {|t| [,]}

      @game.attributes = params[:game]
      @game.teams_games.each { |t| t.attributes = params[:teams_games]
[] }

      if @game.valid? && @game.teams_games.all?(&:valid?)!

        flash[:notice] = "#{} has been updated successfully"
        redirect_to :action => 'list_games', :id => @game.tournament
This topic is locked and can not be replied to.