How to implement a user score by activity

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.

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

2) 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.