Giter Club home page Giter Club logo

Comments (4)

kn avatar kn commented on June 14, 2024

This depends on what you want to do with those positive and negative votes. Also are users allowed to do both positive and negative votes?

from activerecord-reputation-system.

sorens avatar sorens commented on June 14, 2024

This can be done by adding a has_many relationship to you model which points at RSEvaluations. Have you watched/read RyanB's Rails/Ascii-Cast for this gem? If not, it is worthwhile. In any event, if you follow what Ryan suggests, after you've added the :votes reputation to your model, you add a has_many as well.

# app/model/something.rb
has_many :evaluations, :class_name: "RSEvaluation", as: :target

note, I used as: :target instead of as: :source as Ryan did. He was tracking something different. In the case where you are trying to count up the positive and negative votes, the model being voted on is the target.

Then you can add methods to your model to help. For example:

# app/model/something.rb
def count_positive
  evaluations.where( target_type: self.class, target_id: self.id, value: 1.0 ).count
end

Now, when you vote for that something, you can then ask for the count as well

> Something.first.add_or_update_evaluation( :votes, 1, User.first )
> => true
> Something.first.count_positive
> => 1

(note: my above examples have hard-coded objects and made up model names, but hopefully you follow what I am trying to describe? Something is the model you would vote on and User is the model doing the voting. )

from activerecord-reputation-system.

 avatar commented on June 14, 2024

Thank you for this useful comment.
Why don't just write a method like :

              def count_positive
                 self.evaluations.where{(value == "1") }.length
              end

It works well...

from activerecord-reputation-system.

NARKOZ avatar NARKOZ commented on June 14, 2024

Please use Stackoverflow for questions like this next time.

from activerecord-reputation-system.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.