To do what you want to do, you should use has_many :throughinstead hatbm. See here for more details . In short, it’s good that you can add other variables to the connection table. In your case, the boolean name is home_team.
So here is what I will do. First create an association table (since I don’t have much imagination, I will call this participation):
create_table :participations, do |t|
t.integer :game_id, :null => false
t.integer :team_id, :null => false
t.boolean :home_team
end
As you can see, unlike your gamma table, this one has an identifier. And you can add attributes to it. Then I will use these models:
class Participation < ActiveRecord::Base
belongs_to :game
belongs_to :team
end
class Game < ActiveRecord::Base
has_many :participations, :dependent => :destroy
has_many :teams, :through => :participations
end
class Team < ActiveRecord::Base
has_many :participations, :dependent => :destroy
has_many :games, :through => :participations
end
So, to get the teams in the game, you do @game.teams.
, home_team away_team, :
def home_team
self.teams.joins(:participations).where("participations.home_team IS ?", true).first
end
def away_team
self.teams.joins(:participations).where("participations.home_team IS ?", false).first
end
@game.home_team @game.away_team.
Peter edit: , mysql :
self.teams.joins(: ).where( " .home_team =?", true).first self.teams.joins(: ).where( "members.home_team IS NULL" ). first
"=?", true, "! =?", true --OR-- IS NOT NULL IS NULL
, false where("participants.home_team = ?", false)
, , , .
1, , . - :
<%= label_tag :home, 'Home Team' %><br />
<%= label_tag :home_team_1, 'Team 1' %><%= radio_button_tag :home_team, 1 %>
<%= label_tag :home_team_2, 'Team 2' %><%= radio_button_tag :home_team, 2 %>
, params[:home_team] == 1, - , params[:home_team] == 2, - .
2, - , :
<%= label_tag :name, 'Home Team' %>
<%= text_field_tag :name, nil, :name => "home[]" %>
<%= label_tag :name, 'Away Team' %>
<%= text_field_tag :name, nil, :name => "away[]" %>
, -
@game = Game.new(params[:game])
home = Team.create(params[:home])
# or
home = Team.find_or_create_by_name(params[:home][:name])
@game.participations.create(:team_id => home.id, :home_team => true or 1)
away = Team.find_or_create_by_name(params[:away][:name])
@game.participations.create(:team_id => away.id, :home_team => false or 0)