首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用ActiveRecord,如何编写只包含值不为空/nil的对象的where语句?

使用ActiveRecord,如何编写只包含值不为空/nil的对象的where语句?
EN

Stack Overflow用户
提问于 2013-10-03 05:13:57
回答 1查看 47关注 0票数 0

标题不是太多解释,但基本上我有一个游戏模型,每个游戏的has_many统计。这些统计数据也是一个运动员,谁has_many游戏。每个Stat都有一个Stat_Type,我在下面的代码中收集所需的StatTypes:

代码语言:javascript
运行
复制
@games = Athlete.first.games
unless @games.blank?
    @sport_position_stat_types = @games.first.stats.not_calculated.order("id ASC").collect(&:stat_type)
end

这很好用,但我需要一种方法来删除这个列表中的任何stat_type,在任何有值的游戏中都没有该stat_type的统计信息。

基本上,我现在已经拥有了所需的所有stat_types,但我需要将@sport_position_stat_types限制为只包含该运动员的游戏集合中具有值的统计信息的stat_types。

所以这是我需要的:

代码语言:javascript
运行
复制
Athlete
-->Games
  -->Stats
    -->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)

我希望这能很好地解释它。如有任何帮助,我们不胜感激!

编辑::

这是返回的内容(@sport_position_stat_types):

代码语言:javascript
运行
复制
[#<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>, 
#<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>, #<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>,
 #<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>, #<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>, 
#<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>, 
#<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>, 
#<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>, 
#<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>, 
#<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>, 
#<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>]
EN

Stack Overflow用户

发布于 2013-10-04 02:40:08

试试这个:

代码语言:javascript
运行
复制
@games = Athlete.first.games
unless @games.blank?
    @sport_position_stat_types = @games.first.stats.not_calculated.order("id ASC").collect(&:stat_type)
    @sport_position_stat_types.reject(&:blank?)
end
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19146937

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档