@venues 変数を 'merit_scores_override.rb' に渡し、SQL を使用してそのシーズンに関連付けられた会場に基づいて分類する方法を理解しようとしています。したがって、リーダーボードに「since_date」のパラメーターがある場合、そのシーズンのすべての会場が取得され、それらのポイントが加算されます。
現在、シーズンに関連する会場が 2 つしかない場合でも、すべての会場のポイントが加算されます。
http://i62.tinypic.com/fjoxtc.png
http://i62.tinypic.com/mmstn9.png
これらの会場には関連付けられたポイントがないため、最初の画像は 0 である必要があります。
StaticPages#リーダーボード
def leaderboard
@seasons = Season.all
@season = Season.find_all_by_start(params[:since_date])
if(params.has_key?(:since_date))
@venues = Season.find_by_start(params[:since_date]).venues
else
@venues = Venue.all
end
since_date = params[:since_date]
end_date = params[:end_date]
category = params[:category]
if since_date.blank? && end_date.blank? && category.blank?
@scores = Merit::Score.top_scored
elsif since_date.blank? && end_date.blank?
@scores = Merit::Score.top_scored(category: category)
elsif category.blank?
@scores = Merit::Score.top_scored(since_date: since_date, end_date: end_date, venues: @venues)
else
@scores = Merit::Score.top_scored(since_date: since_date, end_date: end_date, category: category, venues: @venues)
end
end
Merit_scores_override.rb
def self.top_scored(options = {})
options[:table_name] ||= :users
options[:since_date] ||= 10.years.ago
options[:end_date] ||= 1.month.from_now
options[:category] ||= nil
options[:limit] ||= 25
#options[:venues] ||= nil
alias_id_column = "#{options[:table_name].to_s.singularize}_id"
if options[:table_name] == :sashes
sash_id_column = "#{options[:table_name]}.id"
else
sash_id_column = "#{options[:table_name]}.sash_id"
end
# MeritableModel - Sash -< Scores -< ScorePoints
sql_query = "SELECT
#{options[:table_name]}.id AS #{alias_id_column},
SUM(num_points) as sum_points
FROM #{options[:table_name]}
LEFT JOIN merit_scores ON merit_scores.sash_id = #{sash_id_column}
LEFT JOIN merit_score_points ON merit_score_points.score_id = merit_scores.id
WHERE merit_score_points.created_at > '#{options[:since_date]}' AND merit_score_points.created_at < '#{options[:end_date]}' "
if(options[:category] != nil)
sql_query += "AND merit_scores.category = \"#{options[:category]}\" "
#else
# options[:venues].each do |venue|
# sql_query += "AND merit_scores.category = \"#{venue.name}\" "
# end
end
sql_query += "GROUP BY #{options[:table_name]}.id, merit_scores.sash_id
ORDER BY sum_points DESC
LIMIT #{options[:limit]} "
results = ActiveRecord::Base.connection.execute(sql_query)
results.map do |h|
h.keep_if { |k, v| (k == alias_id_column) || (k == 'sum_points') }
end
results
end
Ruby on Rails はまだ初めてなので、何でも役に立ちます。お時間をいただき誠にありがとうございました。これが完全に説明されたことを願っています。コメントアウトされたコードは機能しませんでしたが、私が何をしようとしているのかがわかります。