我试图了解如何将我的@venues 变量传递到“merit_scores_override.rb”并使用 SQL 根据与该季节相关的场地进行分类。因此,当排行榜的参数为“since_date”时,它会占用该赛季的所有场地并将这些积分相加。
目前,即使本赛季只有 2 个相关的场馆,它也会累加所有场馆的积分。
http://i62.tinypic.com/fjoxtc.png
http://i62.tinypic.com/mmstn9.png
第一个图像应该是 0,因为这些场所没有与之关联的点。
静态页面#排行榜
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
优点分数覆盖.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 来说仍然很新,任何东西都有帮助。非常感谢您的时间,并希望这已得到彻底解释。注释掉的代码不起作用,但让您了解我正在尝试做什么。