Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The main issue is you don't have access to users who have an existing rank but were not active for the current week in <code>@user_with_points</code> for the given <code>GameType</code>. </p> <p>Why not leave this as is but utilize the <code>updated_at</code> timestamp. When making a call to your <code>GameTypeRank</code> object just pull the ones that were updated in the past week.</p> <pre><code># some controller GameTypeRank.where("id = ? and updated_at &gt;= ?", id, 1.week.ago) </code></pre> <p>However with this you lose the ability to set the old raked users weekly_points to 0.</p> <p>An alternative is to loop through all users that have points for the given GameType regardless of time, like so:</p> <pre><code># user.rb: def self.update_game_type_weekly_rank @game_types = GameType.all @game_types.each do |game_type| @user_with_points = Point.where("game_type_id = ?", game_type.id).sum(:points, :group =&gt; :user_id, :order =&gt; 'sum(points) desc') rank = point_counter = 0 @user_with_points.each do |user_id, points| @game_type_rank = GameTypeRank.find_or_create_by_user_id_and_game_type_id(user_id, game_type.id) if points != point_counter point_counter = points rank += 1 end @game_type_rank.weekly_rank = rank @game_type_rank.weekly_points = points @game_type_rank.save end end end end </code></pre> <p>After this your table should be the way you want it. Now this comes at a cost as you probably realize. Maybe you could have some sort of cut off from inactivity. Say for example 6 months.</p> <pre><code># users.rb .... @user_with_points = Point.where("game_type_id = ? and created_at &gt;= ?", game_type.id, 6.months.ago).sum(:points, :group =&gt; :user_id, :order =&gt; 'sum(points) desc') .... # some controller GameTypeRank.where("id = ? and created_at &gt;= ?", id, 6.months.ago) </code></pre> <p>Both these methods have some inefficiencies in them but it gets the job done. Plus you are probably running this method with a cron job and/or a background job so the effect on your users should be nil. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload