所以我試圖對模型偉大的granchildren執行查詢。的關係是如此...Rails 3檢索所有偉大的孫子記錄作爲ActiveRecord收集
錦標賽>比賽>比賽>玩家
和比賽模式:
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
有沒有辦法找到所有對於任何給定賽事曾孫(播放器)記錄的無陣列所以我可以繼續調節記錄?
爾加,它是如此簡單!難怪我沒有找到....事後看來,我意識到我也可以簡單地完成'@results = Player.join(:match =>:competition)@results = @ results.where(「competition.tournament_id = ?「,@ tournament.id)',但你的方法看起來更乾淨。 – Noz
不客氣。 :) – Chamnap