Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 Retrieving All Great Grandchild Records As ActiveRecord Collection

So I'm trying to perform queries on a models great granchildren. The relationship is as so...

Tournament > Competitions > Matches > Players

and the Tournament model:

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

end

Currently what I have for retrieving all the great great grandchildren records is:

@results = @tournament.competitions.collect{|b| b.matches.collect{|c| c.players.with_win_count}}.flatten

And this works, however the problem is that it returns an array. What I'm trying to do is build a dynamic query based on user input, and the player table is essentially a result pool for all matches and the user has the option to filter only what he wants to see. What I have in the end is a fairly potentially complex (depending on user input) query and the additional where clauses cannot be performed on an array. To give you a better idea of how this is meant to work, here is my code...

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

Is there a way to find all of the great grandchildren (player) records for any given tournament without an array so I can continue to condition the records?

like image 828
Noz Avatar asked Jan 17 '23 13:01

Noz


1 Answers

I think you should just call @tournament.players will work out of the box if you have setup with the above associations.

like image 176
Chamnap Avatar answered Jan 31 '23 08:01

Chamnap