# How to implement a user score by activity

**URL:** <https://www.ruby-forum.com/t/how-to-implement-a-user-score-by-activity/190524>\
**Category:** Rails\
**Created:** [June 22, 2010, 3:35am UTC](https://www.ruby-forum.com/t/how-to-implement-a-user-score-by-activity/190524 "2010-06-22T03:35:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rodrigo3n](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@rodrigo3n](https://www.ruby-forum.com/u/rodrigo3n)\
**Post date:** [June 22, 2010, 3:35am UTC](https://www.ruby-forum.com/t/how-to-implement-a-user-score-by-activity/190524/1 "2010-06-22T03:35:39Z")

</div>

Hello everyone, I gotta start an app that have to give icons to users  
conform they use the application. For example, a user has bought 10  
products so he gets icons symbolizing his activity. A user who has  
bought 5 items has 2 icons and a user who bought 15 has more. The more  
the use the app, the more the icons they get. My doubt is, how to  
implement this? Does anyone have a solution, I can’t imagine how to  
implement this, any gem or normal Rails approach?

Thanks for your attention,  
Rodrigo Alves.

---

<div class="post-metadata">

**Author:** ![rodrigo3n](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@rodrigo3n](https://www.ruby-forum.com/u/rodrigo3n)\
**Post date:** [June 22, 2010, 1:44pm UTC](https://www.ruby-forum.com/t/how-to-implement-a-user-score-by-activity/190524/2 "2010-06-22T13:44:10Z")

</div>

rodrigo3n wrote:

> Hello everyone, I gotta start an app that have to give icons to users  
> conform they use the application. For example, a user has bought 10  
> products so he gets icons symbolizing his activity. A user who has  
> bought 5 items has 2 icons and a user who bought 15 has more. The more  
> the use the app, the more the icons they get. My doubt is, how to  
> implement this? Does anyone have a solution, I can’t imagine how to  
> implement this, any gem or normal Rails approach?
> 
> Thanks for your attention,  
> Rodrigo Alves.

2 approaches i can think of.

1. simple but not very efficient approach: when you render out the  
view, make a decision at render time whether the user has earned an  
award. You could do a helper for this pretty easily, eg

def usage\_icon\_for(user)  
bought = user.bought\_products.size #replace with your logic  
if bought \> 20  
image\_tag “icons/bought\_20.png”  
elsif bought \> 10  
image\_tag “icons/bought\_10.png”  
else  
nil #could replace with a different icon  
end  
end

1. Nicer, more efficient and more flexible way: create a model for the  
‘achievements’, and another for the join table to join achievements to  
users (which you could call achievement\_awards), and have the icons  
represent the achievements. Ie

User  
has\_many :achievement\_awards  
has\_many :achievements, :through =\> :achievement\_awards

AchievementAward  
#models the awarding of an achievement to a user  
#needs user\_id & achievement\_id fields, plus created\_at  
belongs\_to :user  
belongs\_to :award

Achievement  
#fields could be- name, description, icon, where icon is the path to  
the icon graphic (relative to the images folder)

Now, you need to put hooks into your code to award these achievements  
when the user does stuff. Use after\_save for example on the model which  
represents the user buying a product.
