Rails Ruby merit gem观察者无法正常工作

时间:2014-07-09 06:02:45

标签: ruby-on-rails ruby ruby-on-rails-3 merit-gem

我在我的rails项目中使用merit gem并且优点观察者根本不工作。 我的app / observers /文件夹中有Reputation_change_observer.rb

#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

当声誉发生变化时,我正在更新用户的生命时间点。如果用户点达到某一点,则给出徽章。但这两个功能都不起作用。任何人有任何想法?会是什么问题?

1 个答案:

答案 0 :(得分:1)

在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

在config / application.rb(Activate reputation_observer)

config.active_record.observers = :reputation_observer