Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Ruby merit gem observer is not working

I am using merit gem in my rails project and the merit observer is not working at all. I have reputation_change_observer.rb in my app/observers/ folder

#reputation_change_observer.rb
class ReputationChangeObserver
observe :user
def update(changed_data)
   description = changed_data[:description]

   # If user is your meritable model, you can query for it doing:
   user = User.where(sash_id: changed_data[:sash_id]).first

   user.update_life_time_point
   user.give_badges

   # When did it happened:
   datetime = changed_data[:granted_at]
  end
end

When the reputation changes I am updating the life time point of the user. And gives the badges if the user point reaches certain point. But these two functionalities are not working. Anyone has any idea? what would be the issue?

like image 704
Abhi Avatar asked Feb 03 '26 13:02

Abhi


1 Answers

In app/observers/reputation_observer.rb

class ReputationObserver < ActiveRecord::Observer

  def after_update(reputation)
    # use reputation.previous_changes to get the last changes in reputation object and accordingly write your logic
  end

end

In config/application.rb (Activate reputation_observer)

config.active_record.observers = :reputation_observer
like image 143
Allerin Avatar answered Feb 05 '26 06:02

Allerin