I am building an achievement system and the final part is notifying the user. Once the achievement is created in the model
class Achievement < ActiveRecord::Base
def self.check_conditions_for(user)
if user.month_views >= 30 and !user.views_awarded?(self)
user.award(self)
end
end
end
I need to flash a custom notice to the user which they can then close. What is the best way to initiate this from the model? I know I can only call Flash messages from the controller but I need a workaround in this instance. It will look very similar to how stack overflow displays their badges.
Ok so if a user views a video — presumably hitting some url, you should just call
Achievement.check_conditions_for(user)in the associated action.Achievement.check_conditions_for(user)can return some value – maybe a notice message – which you can send as a normal flash message. No need for a workaround here if this is just executed when some action is called.For example, if you have a controller
Videosand you’re calling the actionwatch:in app/views/videos/watch.js.erb: