所以我试图对模型的曾孙执行查询。关系是这样的……

锦标赛 > 比赛 > 比赛 > 球员

和锦标赛模型:

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

end

目前,我用于检索所有曾曾孙记录的是:
@results = @tournament.competitions.collect{|b| b.matches.collect{|c| c.players.with_win_count}}.flatten

这有效,但问题是它返回一个数组。我想要做的是根据用户输入构建动态查询,而玩家表本质上是所有比赛的结果池,用户可以选择仅过滤他想看到的内容。我最后得到的是一个相当复杂(取决于用户输入)的查询,并且不能在数组上执行额外的 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

有没有办法在没有数组的情况下找到任何给定锦标赛的所有曾孙(玩家)记录,以便我可以继续调整记录?

最佳答案

我认为如果你已经设置了上述关联,你应该直接调用 @tournament.players 即可。

关于arrays - Rails 3 检索所有曾孙记录作为 ActiveRecord 集合,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10821536/

10-12 19:59