Note that there are some explanatory texts on larger screens.

plurals
  1. POWith ActiveRecord, how do I write a where statement that only includes objects whose values aren't blank/nil?
    primarykey
    data
    text
    <p>The title isn't too explanatory, but basically I have a Game model, and each game has_many Stats. These stats also belong_to an Athlete, who has_many Games. Each Stat has a Stat_Type, and I'm collecting the StatTypes that I need in the following code:</p> <pre><code>@games = Athlete.first.games unless @games.blank? @sport_position_stat_types = @games.first.stats.not_calculated.order("id ASC").collect(&amp;:stat_type) end </code></pre> <p>This works fine, but I need a way to remove any stat_type in this list where there are NO stats of that stat_type in ANY of the games that have a value.</p> <p>Basically, I have all the stat_types I need right now, but I need to limit @sport_position_stat_types to only stat_types that have stats with values in that athlete's games collection.</p> <p>So here's what I need:</p> <pre><code>Athlete --&gt;Games --&gt;Stats --&gt;Stat_Types (I need all of these as long as there is at least ONE stat of that stat_type that is in the above Athlete's Games, and doesn't have a nil Value column--aka it has a value) </code></pre> <p>I hope this explains it well enough. Any help is appreciated!</p> <p>EDIT::</p> <p>This is what is returned (@sport_position_stat_types):</p> <pre><code>[#&lt;StatType id: 270, sport_id: 6, name: "Return Touchdowns", unit: "", created_at: "2012-12-31 19:20:37", updated_at: "2013-04-29 15:19:59", lower_is_better: false, sport_position_id: 5, display_as_decimal: false, game: true, placement: 28, stat_type_category_id: 1, abbreviation: nil, calculated: false&gt;, #&lt;StatType id: 269, sport_id: 6, name: "Receiving Touchdowns", unit: "", created_at: "2012- 12-31 19:20:10", updated_at: "2013-04-29 15:19:59", lower_is_better: false, sport_position_id: 5, display_as_decimal: false, game: true, placement: 29, stat_type_category_id: 1, abbreviation: nil, calculated: false&gt;, #&lt;StatType id: 268, sport_id: 6, name: "Yards Per Catch", unit: "", created_at: "2012-12-31 19:18:33", updated_at: "2013-04-29 15:19:59", lower_is_better: false, sport_position_id: 5, display_as_decimal: true, game: true, placement: 30, stat_type_category_id: 1, abbreviation: nil, calculated: false&gt;, #&lt;StatType id: 267, sport_id: 6, name: "Receiving Yards", unit: "", created_at: "2012-12-31 19:18:00", updated_at: "2013-04-29 15:19:59", lower_is_better: false, sport_position_id: 5, display_as_decimal: false, game: true, placement: 31, stat_type_category_id: 1, abbreviation: nil, calculated: false&gt;, #&lt;StatType id: 266, sport_id: 6, name: "Catches", unit: "", created_at: "2012-12-31 19:16:24", updated_at: "2013-04-29 15:19:59", lower_is_better: false, sport_position_id: 5, display_as_decimal: false, game: true, placement: 32, stat_type_category_id: 1, abbreviation: nil, calculated: false&gt;, #&lt;StatType id: 265, sport_id: 6, name: "Fumbles", unit: "", created_at: "2012-12-31 19:09:41", updated_at: "2013-04-29 15:19:59", lower_is_better: true, sport_position_id: 5, display_as_decimal: false, game: true, placement: 33, stat_type_category_id: 1, abbreviation: nil, calculated: false&gt;, #&lt;StatType id: 264, sport_id: 6, name: "Rushing Touchdowns", unit: "", created_at: "2012-12- 31 19:09:10", updated_at: "2013-08-20 15:48:48", lower_is_better: false, sport_position_id: 5, display_as_decimal: false, game: true, placement: 34, stat_type_category_id: 4, abbreviation: nil, calculated: false&gt;, #&lt;StatType id: 263, sport_id: 6, name: "Rush Long", unit: "", created_at: "2012-12-31 19:08:42", updated_at: "2013-08-20 15:48:48", lower_is_better: false, sport_position_id: 5, display_as_decimal: false, game: true, placement: 35, stat_type_category_id: 4, abbreviation: nil, calculated: false&gt;, #&lt;StatType id: 262, sport_id: 6, name: "Rush Yards per Carry", unit: "yard", created_at: "2012-12-31 19:06:49", updated_at: "2013-08-20 15:48:48", lower_is_better: false, sport_position_id: 5, display_as_decimal: true, game: true, placement: 36, stat_type_category_id: 4, abbreviation: nil, calculated: false&gt;, #&lt;StatType id: 261, sport_id: 6, name: "Rush Yards", unit: "yard", created_at: "2012-12-31 19:06:01", updated_at: "2013-08-20 15:48:47", lower_is_better: false, sport_position_id: 5, display_as_decimal: false, game: true, placement: 37, stat_type_category_id: 4, abbreviation: nil, calculated: false&gt;, #&lt;StatType id: 260, sport_id: 6, name: "Rushes", unit: "", created_at: "2012-12-31 19:05:24", updated_at: "2013-08-20 15:48:48", lower_is_better: false, sport_position_id: 5, display_as_decimal: false, game: true, placement: 38, stat_type_category_id: 4, abbreviation: nil, calculated: false&gt;] </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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