Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails包含在include上带有条件的记录_Ruby On Rails_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails Rails包含在include上带有条件的记录

Ruby on rails Rails包含在include上带有条件的记录,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我有一个返回activerecord结果的函数,如下所示: def athlete_statistics(game_id) athletes.includes(:statistics).where('statistics.game_id = ?', game_id) end 这将返回特定比赛的所有运动员及其统计数据。但是,它删除了那些没有比赛统计数据的运动员,我想把他们包括在内 我试图这样解决问题: def athlete_statistics(game_id) athletes.in

我有一个返回activerecord结果的函数,如下所示:

def athlete_statistics(game_id)
  athletes.includes(:statistics).where('statistics.game_id = ?', game_id)
end
这将返回特定比赛的所有运动员及其统计数据。但是,它删除了那些没有比赛统计数据的运动员,我想把他们包括在内

我试图这样解决问题:

def athlete_statistics(game_id)
  athletes.includes(:statistics).where('statistics.game_id = ?', game_id) | athletes
end
这将在最后添加缺少的运动员,并返回正确的结果集,但是当我调用
对结果执行_json
操作时:

<%= @games.first.athlete_statistics.to_json(
    :include => [:user, :statistics => {:include => :attribute}]
).html_safe %>

因为它不包括所有与其他比赛相关的运动员。我需要由
include

提供的包含式左外联接,如何尝试:运动员。包括(:统计)。其中('statistics.game\u id=?',game\u id)+运动员。其中('statistics.game\u id!=?',game\u id)@Yarneo,该查询的第二部分无效,因为运动员没有名为'statistics'的列,“where子句在第一部分中起作用,因为include.think是。。。哦,好吧,如果有办法在第二部分添加statistics=>null…这仍然没有帮助,因为运动员仍然可以有其他的统计数据,只是不适用于那场比赛,请参阅我在我的原始帖子“UPDATE”下写的内容。我知道这一点,但你想向运动员展示给定比赛的统计数据,同时也显示其他运动员,但不要显示他们的统计数据,因为他们在给定的比赛中没有任何数据。因此,如果您可以为第二部分执行类似于a删除.includes(:statistics).where('statistics.game\u id!=?',game\u id.).update\u all(“statistics=NULL”)的操作,那么输出的统计字段将临时显示NULL。不确定语法,但从想法上看
athletes.includes(:statistics).where('statistics.game_id = ? OR statistics.game_id IS NULL', game_id)