Note that there are some explanatory texts on larger screens.

plurals
  1. PORails: Bug in Logic Related to Rails Query
    primarykey
    data
    text
    <p>I have an app where Users receive Points for Games. I have a bug in my code which is meant to subtotal and rank each Users' Points by Game_Type for the past week (in a GameTypeRank table). This works fine if the User has Points for the past week, however, it doesn't work correctly if the User has no Points for the past week. If the User has no Points, they should be updated in the GameTypeRank with 0 points otherwise, they retain their last ranking until updated (and the ranks are incorrect).</p> <pre><code># user.rb: def self.update_game_type_weekly_rank @game_types = GameType.all @game_types.each do |game_type| # this query is where the bug is since it is possible a User has no Points @user_with_points = Point.where("game_type_id = ? and created_at &gt;= ?", game_type.id, 1.week.ago).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 # Models # game_type_rank.rb # fields - user_id, game_type_id, weekly_points, weekly_rank belongs_to :game_type belongs_to :user # point.rb # fields - user_id, points, game_type_id belongs_to :game belongs_to :game_type belongs_to :user </code></pre> <p>I could create a method that runs weekly and creates Point records (with Point.points = 0) for each User that didn't have any for the week, but that is a poor solution. </p> <p>I could also maybe initialize the <code>weekly_points</code> and <code>weekly_rank</code> for each GameTypeRank record at the beginning of the method, but that seems inefficient to me (and I'm not sure of the best way to do this).</p> <pre><code># Sample Data Points |user_id|points|game_type_id|created_at |1 | 10 | 1|2013-05-07 |1 | 10 | 2|2013-05-07 |2 | 20 | 2|2012-12-31 |1 | 5 | 2|2012-12-31 before weekly_update - Game_Type_Ranks |user_id |game_type_id | weekly_points | weekly_rank| |1 |2 |5 |2 |2 |2 |20 |1 after weekly_update - Game_Type_Ranks |user_id |game_type_id | weekly_points | weekly_rank| |1 |1 |10 |1 |1 |2 |10 |1 |2 |2 |20 |1 what should happen after weekly_update - Game_Type_Ranks |user_id |game_type_id | weekly_points | weekly_rank| |1 |1 |10 |1 |1 |2 |10 |1 |2 |2 |0 |2 &lt;== update for user_id 2 </code></pre>
    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.
 

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