Rails 3将所有Great孙子记录检索为ActiveRecord Collection

Noz*_*Noz 4 arrays join dynamicquery grandchild ruby-on-rails-3

所以我正在尝试对模型伟大的granchildren执行查询.关系如此......

比赛>比赛>比赛>球员

和锦标赛模型:

class Tournament < ActiveRecord::Base
  has_many :competitions, :dependent => :destroy
  has_many :matches, :through => :competitions
  has_many :players, :through => :matches

end
Run Code Online (Sandbox Code Playgroud)

目前我所拥有的用于检索所有伟大的孙子孙记录的是:

@results = @tournament.competitions.collect{|b| b.matches.collect{|c| c.players.with_win_count}}.flatten
Run Code Online (Sandbox Code Playgroud)

这是有效的,但问题是它返回一个数组.我要做的是基于用户输入构建动态查询,并且播放器表本质上是所有匹配的结果池,并且用户可以选择仅过滤他想要看到的内容.我最终得到的是一个相当复杂的(取决于用户输入)查询以及无法在数组上执行的附加where子句.为了让您更好地了解这是如何工作的,这是我的代码......

def results
   @tournament = Tournament.find(params[:id])
   @results = @tournament.all_great_grandchildren
   @results.where(params[:player_condition1]) if params[:player_condition1]
   @results.where(params[:player_condition2]) if params[:player_condition2]
   @results.where(params[:player_condition3]) if params[:player_condition3]

   if params[:match_condition]
     #Join to match table so we can query the match fields
     @results.join(:match)
     @results.where(params[:match_condition])
   end
   ....
   @results.order(params[:order]) if params[:order]
end
Run Code Online (Sandbox Code Playgroud)

有没有办法找到没有阵列的任何特定锦标赛的所有伟大的孙子(球员)记录,所以我可以继续调整记录?

Cha*_*nap 8

@tournament.players如果您已经设置了上述关联,我认为你应该打电话就可以开箱即用.