This is my first question on stackoverflow. I've tried to search similar ones but have found nothing so far. Plus, I'm a bit of a newbie as Ruby/Rails developer so maybe I'm completely missing basic concepts.
But let's get to the point.
My app is basically a game. At the end of a game the player, which is a Devise-logged-in user, is shown the score he got.
When the player clicks on a "ranking" button this score is then sent to the server, via JavaScript, as querystring in the URL for the "ranking" page where a top-5 ranking is shown. Along with the score is sent the difficulty of the game that has just ended, so the rank will refer to that level of difficulty.
So we have a call to the "ranking" action of the Pages controller. What I want to do is to update the "max_score" of the User, that is checking if the current score is greater than the "max_score" attribute and consequentially updating the attribute. Since we have these three different levels of difficulty, User actually has three attributes relating to the top-score: max_easy, max_medium, max_hard.
Thus, I first need to check which of the three attributes has to be considered, and then evaluating the "greater-than" condition.
I decided to define a method in the User model, which is called by Pages#ranking. Below is the code:
def update_max_score(diff,score)
@ref_score = self.max_easy if diff == "easy"
@ref_score = self.max_medium if diff == "medium"
@ref_score = self.max_hard if diff == "hard"
if score.to_i > @ref_score
@ref_score= score.to_i
end
end
But the actual instance attribute isn't updated, even calling the method from console. The only way I've found so far to get this method to work is to explicitly use self.max_easy OR self.max_medium, which isn't per se evil but surely implies triplicating the instructions.
Plus, I'd like to learn, for future reference, what's wrong with that assignement: I just can't get it.
Thank you in advance.