Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To do what you want to do, you should use a <code>has_many :through</code> instead of <code>hatbm</code>. See <a href="http://blog.hasmanythrough.com/2006/4/20/many-to-many-dance-off">here</a> for more info. In short, the good thing is that you can add other variables to the joins table. In your case, a boolean called home_team.</p> <p>So here's what I'd do. First, create an association table (since I don't have much imagination, I'll call it participation):</p> <pre><code>create_table :participations, do |t| t.integer :game_id, :null =&gt; false t.integer :team_id, :null =&gt; false t.boolean :home_team end </code></pre> <p>As you can see, unlike your gamesteams table, this one has an id. And you can add attributes to it. Then, I would use these models:</p> <pre><code>class Participation &lt; ActiveRecord::Base belongs_to :game belongs_to :team end class Game &lt; ActiveRecord::Base has_many :participations, :dependent =&gt; :destroy has_many :teams, :through =&gt; :participations end class Team &lt; ActiveRecord::Base has_many :participations, :dependent =&gt; :destroy has_many :games, :through =&gt; :participations end </code></pre> <p>So to get the teams of a game, you do <code>@game.teams</code>.</p> <p>Now, to get home_team and away_team, add these methods to your Game model:</p> <pre><code>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 </code></pre> <p>And then you'll be able to do <code>@game.home_team</code> and <code>@game.away_team</code>.</p> <blockquote> <p>Peter's edit: Ok, so for mysql you'll have to use different where statements:</p> <p>self.teams.joins(:participants).where("participants.home_team = ?", true).first self.teams.joins(:participants).where("participants.home_team IS NULL").first</p> <p>I can either use " = ?", true and "!= ?", true --OR-- IS NOT NULL and IS NULL</p> </blockquote> <p>I think for false you should try using <code>where("participants.home_team = ?", false)</code></p> <p>Ok, so so there are at least 2 ways to set up your teams.</p> <ol> <li>You let the user pick which team is playing home</li> <li>You assume the first team is the home team</li> </ol> <p>If you go for number 1, you should use a radio button to let the user decide. Something like this:</p> <pre><code>&lt;%= label_tag :home, 'Home Team' %&gt;&lt;br /&gt; &lt;%= label_tag :home_team_1, 'Team 1' %&gt;&lt;%= radio_button_tag :home_team, 1 %&gt; &lt;%= label_tag :home_team_2, 'Team 2' %&gt;&lt;%= radio_button_tag :home_team, 2 %&gt; </code></pre> <p>So if <code>params[:home_team] == 1</code>, the first team is the home team, if <code>params[:home_team] == 2</code>, the second team is the home team.</p> <p>If you go for number 2, then, you should have something like this in your form do add the teams to your game:</p> <pre><code> &lt;%= label_tag :name, 'Home Team' %&gt; &lt;%= text_field_tag :name, nil, :name =&gt; "home[]" %&gt; &lt;%= label_tag :name, 'Away Team' %&gt; &lt;%= text_field_tag :name, nil, :name =&gt; "away[]" %&gt; </code></pre> <p>So then in your controller you can do something like</p> <pre><code>@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 =&gt; home.id, :home_team =&gt; true or 1) away = Team.find_or_create_by_name(params[:away][:name]) @game.participations.create(:team_id =&gt; away.id, :home_team =&gt; false or 0) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload