所以我正在尝试对模型伟大的granchildren执行查询.关系如此…… 比赛比赛匹配玩家 和锦标赛模型: class Tournament ActiveRecord::Base has_many :competitions, :dependent = :destroy has_many :matches, :through
比赛>比赛>匹配>玩家
和锦标赛模型:
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如果你已经设置了上述协会,将开箱即用.