Comments (4)
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.
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.
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.
Please use Stackoverflow for questions like this next time.
from activerecord-reputation-system.
Related Issues (20)
- Adding the activerecord-reputation-system makes my other gem to crash HOT 2
- Please remove support 'protected_attributes' HOT 3
- Karma reputation not updating or updating inconsistently HOT 3
- Find reputation by a given source HOT 2
- Get only the first 5 evaluators HOT 1
- How to remove an evaluator that is no longer existing HOT 3
- Get the time of evaluation HOT 1
- Scope by Associations HOT 2
- Can't mass-assign protected attributes: reputation_name, target_id, target_type, aggregated_by
- duplicate key value violates unique constraint "index_rs_evaluations_on_reputation_name_and_source_and_target"
- Need a push to ruby gems...
- In the Wiki there is a wrong link HOT 1
- Add validations in reputation system definitions
- [SOLVED]Use the same reputation name in aggregate of aggregates always returning 0 HOT 1
- data column?
- iterate just over upvoted items
- A source cannot evaluate a target more than once!
- get rank for user doesn't work with postgresql
- Reputation not automatically aggregated
- Zero reputations result in a non zero reputation when sum
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from activerecord-reputation-system.