M1xelated
M1xelated

Reputation: 169

Rails: Allow users only to get points only once. (Prevent users from refreshing page and get points again)

I am creating a course system in rails and I want to award users points for completing a certain task but I want to avoid that the users can refresh the page and get the points again.

My controller looks like this:

def beginnerscourse_08c
   @user = current_user
   @completion = "100%"
   @user.increment(:tradepoints,  100)
   @user.save
end

What is the easiest way to make a boolean or similar system that checks if the user was already awarded theses points and if not reward them.

Upvotes: 0

Views: 94

Answers (1)

Ege Ersoz
Ege Ersoz

Reputation: 6571

You need an association table that has user_id and course_id columns as foreign keys. The purpose of the table is to keep track of which users have received awards for which courses. For example:

user_id    course_id
   1           1
   2           1
   2           2
   3           1
   4           3

Looking at the above table you can clearly see that User 1 has completed Course 1, User 2 has completed Course 1 and 2, and so on. You can then check that table to see if a specific user has received awards for the given course before incrementing their points, as well as do things like show the user a list of courses they have completed, or run reports on how many users have completed a given course.

Upvotes: 3

Related Questions