Question

So I am using the method in: How to implement an achievement system in RoR to implement a badges/achievement system on my app.

This has worked great thus far, but I am wanting to have a "master list" of all the badges that currently exist on the site. As this is continually increasing, I'd like it if this list could dynamically populate itself, instead of me having to update it by hand.

This sounds pretty easy, but because of how the achievements system works (the various types of badges are all subclasses of the Achievement.rb model...there are many instances of the same badges in the db) I am not sure of how to be able to determine all the subclasses of the Achievements model.

Additionally, I would like for each badge to have its own show page (example url: http://www.mysite.com/achievements/badge1)

So within this master list the badge images will look something like this:

<%= link_to "#{image_tag @achievement.photo}", achievement_path(@achievement) %>

However, yet again, I have no idea how to iterate through all the subclasses of the Achievement model.

Does this make sense? How should I go about doing this?

Thanks,

Was it helpful?

Solution

To iterate through the subclasses, you should be able to do something like this:

#Get the subclasses as class objects
Achievement.subclasses

#Get just the subclass names
Achievement.subclasses.map(&:name)

And then for the show URLs, probably make a route like 'achievements/:badge' and, in your controller, do

@badges = Achievement.where(:type => params[:badge]).all

#or, depending on how you've named everything
@badge = params[:badge].camelize.constantize.all
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top