0

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.

1 Answer 1

1

You want to update the column in the database, so you should use ORM method:

def update_max_score(diff, score)
  update_attributes "max_#{diff}" => [send("max_#{diff}"), score.to_i].max
end
Sign up to request clarification or add additional context in comments.

3 Comments

This applies to the OP as well, but self is redundant.
Got the idea and the syntax lying underneath, but the ref_score attribute doesn't exist. Tried to do ref_score = "max_#{diff}" but it still says that ref_score isn't an attribute.
No, there is no ref_score column in DB. What I wanted to do was putting in ref_score the name of the DB column I wanted to update AND then updating it. I had thought of doing, in the Pages#ranking: current_user.update_max_score(params[:diff],params[:score]) current_user.save! so it wasn't necessary for the update_max_score method to actually save to DB... But this way it's no longer needed to explicitly call save! in the Controller! BTW, I've found a way to get it to work: update_attributes( :"max_#{diff}" => [send("max_#{diff}"), score.to_i].max ) which is great, if safe.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.