Rails 3:场地优秀排行榜(HMBTM协会)季节

时间:2014-06-10 00:37:46

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.2 merit-gem

我试图了解如何将我的@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还是一个新手,一切都有帮助。真的很感激你的时间,并希望这是彻底解释。注释掉的代码不起作用,但让你知道我想要做什么。

1 个答案:

答案 0 :(得分:0)

我不知何故在接下来的几天里每次都能回答我自己的问题。我最终使用这些if语句只在​​那个季节内抓住场地或者在个别场地进行。

  if(options[:category] != nil)
    sql_query += "AND merit_scores.category = \"#{options[:category]}\" "
  elsif(options[:venues] != nil)
    sql_venues = Array.new
    sql_query += "AND ("
    options[:venues].each do |venue|
      sql_venues.push("merit_scores.category = \"#{venue.name}\"")
    end
    sql_query += sql_venues.join(" OR ")
    sql_query += ") \n"
  end