Rails 4: Creating point rules using merit gem

1.1k views Asked by At

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?

2

There are 2 answers

2
Billy Chan On BEST ANSWER

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.

4
Justin On

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.