Reputation: 169
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
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