Rails 4: Creating point rules using merit gem

蓝咒 提交于 2019-12-07 10:29:50

问题


I am in the process of converting my app's voting and reputation system from activerecord-reputation-system to merit because the latter seems to have a much more active core team.

I'm attempting to establish point rules for voting on questions and answers in my app. Ideally, when a user votes up a question, the rule would allocate points to the question itself, and also to the question's creator.

From merit's readme section on point rules, I see this example:

score 15, on: 'reviews#create', to: [:reviewer, :reviewed]

My understanding is that :reviewer and reviewed in this example are the ones that get the points allocated to them. However, when I try this in my own point_rules.rb:

score 10, :on => 'questions#vote', :to => :question 

I get the following error:

[merit] NoMethodError on `Question#question` (called from Merit::TargetFinder#other_target)

I know that I am missing something here, but can someone please tell me what it is?


回答1:


Merit works like this:

If you define a rule without specify to whom, the points will be assigned to current_user by default

score 15, on: 'reviews#create' # This is for current_user

If you want to assign points to an user outside of current_user, specify it

score 10, :on => 'questions#vote', to: :user

In above example, :user refers to method question.user, who is the author of the question, different from current_user who voted this.

In OP's case, actually ActiveRecord Reputation and Merit are for different purpose and can't be fully interchanged.

  1. ARP can be used on all models, including users and non-users. Meirt is for user only.

  2. Merit has another module for badge. ARP doesn't.

  3. Merit has a Rule module where you can define all rules in one place, like CanCan. While in ARP there is no such place.

  4. You'll deal with models mainly in ARP. In Merit, you'll work with both Controller and Model.




回答2:


This code works for my project using merit.

score 10, :on => 'questions#vote'

score 10, :on => 'questions#vote', to: :user

I used both because I'm not sure how to combine them. My app involves votes on comments, and the voter will receive 10 points for voting on the comment and the comment owner also receives 10 points.

Hope this helps.



来源:https://stackoverflow.com/questions/20648708/rails-4-creating-point-rules-using-merit-gem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!