Modeling/Validating a two to many Relationship

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
relationship.

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
work.

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
somehow?

Any help would be appreciated.

Thanks,
D.

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

Edit Game Form

[code=]

Edit Game Details

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

Tournament <%= select :game, :tournament_id, @tournaments %>

Name <%= text_field :game, :name %>

Date <%= date_select :game, :date %>

Home and Guest Teams

<% for @teams_games in @game.teams_games %> <%= error_messages_for :teams_games %>

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

<%= submit_tag ‘Update’ %>
<%= link_to ‘Cancel’, :action => ‘list_games’, :id =>
@game.tournament %>
<% end %>[/code]
The models

[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
end

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
end

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”}
end
end[/code]
The edit action

[code=]def edit_game
@tournaments = Tournament.find(:all, :order => ‘name’).map {|t|
[t.name, t.id]}
@game = Game.find(params[:id])
@teamslist = Team.find(:all, :order => ‘name’).map {|t| [t.name,
t.id]}

if request.post?
  @game.attributes = params[:game]
  @game.teams_games.each { |t| t.attributes = params[:teams_games]

[t.id.to_s] }

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

    flash[:notice] = "#{@game.name} has been updated successfully"
    redirect_to :action => 'list_games', :id => @game.tournament
  end
end

end[/code]