3

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?

Abhi
  • 3,361
  • 2
  • 33
  • 38
  • What version of merit are you using? Did you follow the instructions in https://github.com/tute/merit#getting-notifications? Very recently I did a related bug fix release, 2.1.2, if you can upgrade it will get easier. – TuteC Jul 10 '14 at 17:57

1 Answers1

1

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
Allerin
  • 1,108
  • 10
  • 7
  • Actually I forgot to add this line in application.rb. I added it. After that I got a NameError: `uninitialized constant ReputationChangeObserver (NameError)` I added observe :user in the observer to fix it. But the merit functionality is not working at all. – Abhi Jul 09 '14 at 07:34
  • I had added the above code related to ActiveRecord::Observer, but you are using merit gem. In this case you will need to add config.add_observer 'ReputationChangeObserver' # In `config/initializers/merit.rb` – Allerin Jul 09 '14 at 07:47