Cameron Aziz Cameron Aziz - 6 months ago 19
Ruby Question

Display Unique Results from Two Models

I have two models:

games
and
pickems
.

Here is my schema for these models:

create_table "games", force: :cascade do |t|
t.integer "week_id", limit: 4
t.integer "home_team_id", limit: 4
t.integer "away_team_id", limit: 4
t.integer "home_score", limit: 4
t.integer "away_score", limit: 4
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "season_id", limit: 4
end

create_table "pickems", force: :cascade do |t|
t.integer "user_id", limit: 4
t.integer "game_id", limit: 4
t.integer "winner_id", limit: 4
t.integer "score", limit: 4
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end


Game model:

class Game < ActiveRecord::Base
belongs_to :home_team, class_name: 'Team'
belongs_to :away_team, class_name: 'Team'
belongs_to :week
belongs_to :season
end


Pickem model:

class Pickem < ActiveRecord::Base
has_one :user
has_one :game
has_one :winner, class_name: 'Team'
end


In my view, I want to display all Games that do not have a Pickem associated that is referencing it. I also want to display below all the Pickems and the game attributes associated. What do I need to be calling from the controller and/or add to the model(s) to display this information?

Answer

You will need to correct the Games/Pickem association, by adding this to the Game model:

has_one :pickem

and this to the Pickem model:

belongs_to :game

This query will retrieve all of the Games that do not have a Pickem associated:

@games_without_pickems = Game.joins(:pickem).group("games.id").having("COUNT('pickems.id') = 0").order("games.id")

change table references

This will retrieve all of the Pickems and associated Games information:

@pickems = Pickem.includes(:games).all

In your view, simply loop over both @games_without_pickems and @pickems, like this:

<table>
    <th>
        <td>Week</td>
        <td>Home Team</td>
        <td>Away Team</td>
        <td>Home Score</td>
        <td>Away Score</td>
        <td>Season</td>
    </th>
<% @games_without_pickems.each do |game| %>
    <tr>
        <td><%= game.week.name %></td>
        <td><%= game.home_team.name %></td>
        <td><%= game.away_team.name %></td>
        <td><%= game.home_score %></td>
        <td><%= game.away_score %></td>
        <td><%= game.season.name %></td>
    </tr>
<% end %>
</table>

<table>
    <th>
        <td>User</td>
        <td>Winner</td>
        <td>Score</td>

        <td>Week</td>
        <td>Home Team</td>
        <td>Away Team</td>
        <td>Home Score</td>
        <td>Away Score</td>
        <td>Season</td>
    </th>
<% @pickems.each do |pickem| %>
    <tr>
        <td><%= pickem.user.name %></td>
        <td><%= pickem.winner.name %></td>
        <td><%= pickem.score %></td>

        <td><%= pickem.game.week.name %></td>
        <td><%= pickem.game.home_team.name %></td>
        <td><%= pickem.game.away_team.name %></td>
        <td><%= pickem.game.home_score %></td>
        <td><%= pickem.game.away_score %></td>
        <td><%= pickem.game.season.name %></td>
    </tr>
<% end %>
</table>

That should do it.