gpt4 book ai didi

arrays - Rails 3 检索所有曾孙记录作为 ActiveRecord 集合

转载 作者:行者123 更新时间:2023-12-04 23:06:36 25 4
gpt4 key购买 nike

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

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

和锦标赛模型:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com