Witam. Stworzyłem zapytanie poprzez SQL’a które działa.
SELECT Players.id, Players.name, Players.surname, Players.group_types_id, SUM(Results.result), Results.resultok FROM Players, Results
WHERE Results.player_id = Players.id AND Results.resultok = "t" AND Players.group_types_id = 51
GROUP BY Results.player_id, Players.group_types_id
ORDER BY SUM(Results.result) DESC
Ale teraz musze je zaimplementować poprzez railsy.
@project.results.select("Players.id as player_id", "Players.group_types_id as group_type", "SUM(result) as sum_result_count").joins("JOIN Players").where("Results.player_id = Players.id", :resultok => "t", :group_type => 51).group(:player_id, :group_type).order("sum_result_count DESC").each do |s|
Problem tkwi w tym, że w tym drugim kodzie group_types_id jakby nie jest brane w ogóle pod uwagę. Wyświetla mi wszystko, nie filtrując do 51. Dlaczego?
Railsy wytwarzają takie zapytanie(nie ma na liście group_types nie wiedząc czemu)
SELECT Players.id as player_id, Players.group_types_id as group_type, SUM(result) as sum_result_count FROM "results" JOIN Players WHERE "results"."project_id" = ? AND (Results.player_id = Players.id) GROUP BY player_id, group_type ORDER BY sum_result_count DESC