Array – Rails 3 Retrieves all Great GrandChild records for ActiveRecord Collection

So I’m trying to perform a query on the model’s great granchildren. The relationship is like this…

match>match>match>player

And tournament model:

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

end

The one I currently have for retrieving all the great grandchildren records is :

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

This works, but the problem is that it returns an array. What I want to do is build a dynamic query based on user input, and the player table is essentially a pool of all matching results, and the user can choose to filter only what he wants What I saw. What I ended up with was a rather complex (depending on user input) query and an additional where clause that could not be executed on the array. To give you a better understanding of how this works, here is mine 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 p arams[: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 the great grandchildren (players) records of any particular tournament without an array, so I can continue to adjust the records?

I think you should call @ tournament.players if you have set up the above association, will Out of the box.

So I am trying to perform a query on the model’s great granchildren. The relationship is like this…

match>match>match>player< /p>

And the tournament model:

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

end

Currently what I have is used to retrieve all the great The records of grandchildren are:

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

This works, but the problem is that it returns an array. What I want to do is build a dynamic query based on user input, and the player table is essentially a pool of all matching results, and the user can choose Only filter what he wants to see. What I end up with is a rather complex (depending on user input) query and an additional where clause that cannot be executed on the array. To give you a better understanding of how this works , This 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_condit ion2]) 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 the great grandchildren (players) of any particular tournament without an array ) Records, so I can continue to adjust records?

I think you should call @tournament.players if you have set up the above association, it will work out of the box.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2750 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.