So I have a Results table for a Tournament where multiple matches might occur with the same player.
So some sample data might be:
Results
Player_ID | Match_ID|Win|Elapsed_Time|etc..
1 | 1 |T | 1:00 |etc..
2 | 1 |F | 1:00 |etc..
1 | 2 |T | 3:00 |etc..
3 | 2 |F | 3:00 |etc..
I want to prepare a scope that will count the Win fields that are set to True for each Player and and group by that count field.
So pseudo-code would be something like…
scope :most_wins, :all, :order_by => “Count(Player Wins)”
Is this even possible or should I re-think my database structure?
First and foremost: Named Scopes Are Dead
I suggest you do the following:
In case you want it to be in Results table:
this gonna work but I find it a little bit ugly.
And if you want conditional count in the nested query the simple way would be:
!!! Please note that it is prone to SQL injections if
conditionsargument is going to be directly built of user input.