Giter Club home page Giter Club logo

acts_as_votable's Introduction

Acts As Votable (aka Acts As Likeable)

Build status

Acts As Votable is a Ruby Gem specifically written for Rails/ActiveRecord models. The main goals of this gem are:

  • Allow any model to be voted on, like/dislike, upvote/downvote, etc.
  • Allow any model to be voted under arbitrary scopes.
  • Allow any model to vote. In other words, votes do not have to come from a user, they can come from any model (such as a Group or Team).
  • Provide an easy to write/read syntax.

Installation

Supported Ruby and Rails versions

  • Ruby >= 2.5.0
  • Rails >= 5.1

Install

Just add the following to your Gemfile to install the latest release.

gem 'acts_as_votable'

And follow that up with a bundle install.

Database Migrations

Acts As Votable uses a votes table to store all voting information. To generate and run the migration just use.

rails generate acts_as_votable:migration
rails db:migrate

You will get a performance increase by adding in cached columns to your model's tables. You will have to do this manually through your own migrations. See the caching section of this document for more information.

Usage

Votable Models

class Post < ApplicationRecord
  acts_as_votable
end

@post = Post.new(name: 'my post!')
@post.save

@post.liked_by @user
@post.votes_for.size # => 1

Like/Dislike Yes/No Up/Down

Here are some voting examples. All of these calls are valid and acceptable. The more natural calls are the first few examples.

@post.liked_by @user1
@post.downvote_from @user2
@post.vote_by voter: @user3
@post.vote_by voter: @user4, vote: 'bad'
@post.vote_by voter: @user5, vote: 'like'

By default all votes are positive, so @user3 has cast a 'good' vote for @post.

@user1, @user3, and @user5 all voted in favor of @post.

@user2 and @user4 voted against @post.

Just about any word works for casting a vote in favor or against post. Up/Down, Like/Dislike, Positive/Negative... the list goes on-and-on. Boolean flags true and false are also applicable.

Revisiting the previous example of code.

# positive votes
@post.liked_by @user1
@post.vote_by voter: @user3
@post.vote_by voter: @user5, vote: 'like'

# negative votes
@post.downvote_from @user2
@post.vote_by voter: @user2, vote: 'bad'

# tally them up!
@post.votes_for.size # => 5
@post.weighted_total # => 5
@post.get_likes.size # => 3
@post.get_upvotes.size # => 3
@post.get_dislikes.size # => 2
@post.get_downvotes.size # => 2
@post.weighted_score # => 1

Active Record scopes are provided to make life easier.

@post.votes_for.up.by_type(User)
@post.votes_for.down
@user1.votes.up
@user1.votes.down
@user1.votes.up.for_type(Post)

Once scoping is complete, you can also trigger a get for the voter/votable

@post.votes_for.up.by_type(User).voters
@post.votes_for.down.by_type(User).voters

@user.votes.up.for_type(Post).votables
@user.votes.up.votables

You can also 'unvote' a model to remove a previous vote.

@post.liked_by @user1
@post.unliked_by @user1

@post.disliked_by @user1
@post.undisliked_by @user1

Unvoting works for both positive and negative votes.

Examples with scopes

You can add a scope to your vote

# positive votes
@post.liked_by @user1, vote_scope: 'rank'
@post.vote_by voter: @user3, vote_scope: 'rank'
@post.vote_by voter: @user5, vote: 'like', vote_scope: 'rank'

# negative votes
@post.downvote_from @user2, vote_scope: 'rank'
@post.vote_by voter: @user2, vote: 'bad', vote_scope: 'rank'

# tally them up!
@post.find_votes_for(vote_scope: 'rank').size # => 5
@post.get_likes(vote_scope: 'rank').size # => 3
@post.get_upvotes(vote_scope: 'rank').size # => 3
@post.get_dislikes(vote_scope: 'rank').size # => 2
@post.get_downvotes(vote_scope: 'rank').size # => 2

# votable model can be voted under different scopes
# by the same user
@post.vote_by voter: @user1, vote_scope: 'week'
@post.vote_by voter: @user1, vote_scope: 'month'

@post.votes_for.size # => 2
@post.find_votes_for(vote_scope: 'week').size # => 1
@post.find_votes_for(vote_scope: 'month').size # => 1

Adding weights to your votes

You can add weight to your vote. The default value is 1.

# positive votes
@post.liked_by @user1, vote_weight: 1
@post.vote_by voter: @user3, vote_weight: 2
@post.vote_by voter: @user5, vote: 'like', vote_scope: 'rank', vote_weight: 3

# negative votes
@post.downvote_from @user2, vote_scope: 'rank', vote_weight: 1
@post.vote_by voter: @user2, vote: 'bad', vote_scope: 'rank', vote_weight: 3

# tally them up!
@post.find_votes_for(vote_scope: 'rank').sum(:vote_weight) # => 6
@post.get_likes(vote_scope: 'rank').sum(:vote_weight) # => 6
@post.get_upvotes(vote_scope: 'rank').sum(:vote_weight) # => 6
@post.get_dislikes(vote_scope: 'rank').sum(:vote_weight) # => 4
@post.get_downvotes(vote_scope: 'rank').sum(:vote_weight) # => 4

The Voter

You can have your voters acts_as_voter to provide some reserve functionality.

class User < ApplicationRecord
  acts_as_voter
end

@user.likes @article

@article.votes_for.size # => 1
@article.get_likes.size # => 1
@article.get_dislikes.size # => 0

To check if a voter has voted on a model, you can use voted_for?. You can check how the voter voted by using voted_as_when_voted_for.

@user.likes @comment1
@user.up_votes @comment2
# user has not voted on @comment3

@user.voted_for? @comment1 # => true
@user.voted_for? @comment2 # => true
@user.voted_for? @comment3 # => false

@user.voted_as_when_voted_for @comment1 # => true, user liked it
@user.voted_as_when_voted_for @comment2 # => false, user didnt like it
@user.voted_as_when_voted_for @comment3 # => nil, user has yet to vote

You can also check whether the voter has voted up or down.

@user.likes @comment1
@user.dislikes @comment2
# user has not voted on @comment3

@user.voted_up_on? @comment1 # => true
@user.voted_down_on? @comment1 # => false

@user.voted_down_on? @comment2 # => true
@user.voted_up_on? @comment2 # => false

@user.voted_up_on? @comment3 # => false
@user.voted_down_on? @comment3 # => false

Aliases for methods voted_up_on? and voted_down_on? are: voted_up_for?, voted_down_for?, liked? and disliked?.

Also, you can obtain a list of all the objects a user has voted for. This returns the actual objects instead of instances of the Vote model. All objects are eager loaded

@user.find_voted_items

@user.find_up_voted_items
@user.find_liked_items

@user.find_down_voted_items
@user.find_disliked_items

Members of an individual model that a user has voted for can also be displayed. The result is an ActiveRecord Relation.

@user.get_voted Comment

@user.get_up_voted Comment

@user.get_down_voted Comment

Registered Votes

Voters can only vote once per model. In this example the 2nd vote does not count because @user has already voted for @shoe.

@user.likes @shoe
@user.likes @shoe

@shoe.votes_for.size # => 1
@shoe.get_likes.size # => 1

To check if a vote counted, or registered, use vote_registered? on your model after voting. For example:

@hat.liked_by @user
@hat.vote_registered? # => true

@hat.liked_by => @user
@hat.vote_registered? # => false, because @user has already voted this way

@hat.disliked_by @user
@hat.vote_registered? # => true, because user changed their vote

@hat.votes_for.size # => 1
@hat.get_positives.size # => 0
@hat.get_negatives.size # => 1

To permit duplicates entries of a same voter, use option duplicate. Also notice that this will limit some other methods that didn't deal with multiples votes, in this case, the last vote will be considered.

@hat.vote_by voter: @user, duplicate: true

Caching

To speed up perform you can add cache columns to your votable model's table. These columns will automatically be updated after each vote. For example, if we wanted to speed up @post we would use the following migration:

class AddCachedVotesToPosts < ActiveRecord::Migration
  def change
    change_table :posts do |t|
      t.integer :cached_votes_total, default: 0
      t.integer :cached_votes_score, default: 0
      t.integer :cached_votes_up, default: 0
      t.integer :cached_votes_down, default: 0
      t.integer :cached_weighted_score, default: 0
      t.integer :cached_weighted_total, default: 0
      t.float :cached_weighted_average, default: 0.0
    end

    # Uncomment this line to force caching of existing votes
    # Post.find_each(&:update_cached_votes)
  end
end

If you have a scope for your vote, let's say subscribe, your migration will be slightly different like below:

class AddCachedVotesToPosts < ActiveRecord::Migration
  def change
    change_table :posts do |t|
      t.integer :cached_scoped_subscribe_votes_total, default: 0
      t.integer :cached_scoped_subscribe_votes_score, default: 0
      t.integer :cached_scoped_subscribe_votes_up, default: 0
      t.integer :cached_scoped_subscribe_votes_down, default: 0
      t.integer :cached_weighted_subscribe_score, default: 0
      t.integer :cached_weighted_subscribe_total, default: 0
      t.float :cached_weighted_subscribe_average, default: 0.0

      # Uncomment this line to force caching of existing scoped votes
      # Post.find_each { |p| p.update_cached_votes("subscribe") }
    end
  end
end

cached_weighted_average can be helpful for a rating system, e.g.:

Order by average rating:

Post.order(cached_weighted_average: :desc)

Display average rating:

<%= post.weighted_average.round(2) %> / 5
<!-- 3.5 / 5 -->

Votable model's updated_at

You can control whether updated_at column of votable model will be touched or not by passing cacheable_strategy option to acts_as_votable method.

By default, update strategy is used. Pass :update_columns as cacheable_strategy if you don't want to touch model's updated_at column.

class Post < ApplicationRecord
  acts_as_votable cacheable_strategy: :update_columns
end

Testing

All tests follow the RSpec format and are located in the spec directory. They can be run with:

rake spec

License

Acts as votable is released under the MIT License.

acts_as_votable's People

Contributors

anothermh avatar astratto avatar cheeseweasel avatar ejholmes avatar epergo avatar fatkodima avatar fltiago avatar hafizbadrie avatar jeewes avatar kondratenko-valeriy avatar leizzer avatar michaelwhi avatar mnort9 avatar morr avatar murny avatar olleolleolle avatar petergoldstein avatar phlipper avatar polopi avatar reneklacan avatar ryanto avatar sbycrosz avatar sj26 avatar strivedi183 avatar vala avatar vietqhoang avatar xenleme avatar ybakos avatar yshmarov avatar zitzabis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

acts_as_votable's Issues

undefined method "voted_for?" heroku+authlogic+acts_as_votable

Just updated a little things in my app and this appeared in production
https://gist.github.com/4652260

Any idea of what happened? it is working fine on development and production on my computer, but when i put it in production on heroku... it crashes.

Tell me what info do you need to know what is happening.

In this app I'm using ruby 1.9.2, rails 3.2.3, heroku for production enviorment and postgresql 9.1 for the databases.

Thanks in advance.

LucasPadovan - Mendoza, Argentina

List users who voted for model

Hello,

Firstly apologies for posting here I tried stack overflow, I know its not an issue, I want show the users who voted for a given model.

I have a post model which can be voted for by users. When I do the typical

<%= div_for @post do %>
<%= @post.name %>
ect ect

<% end %>

I want to also show a list of users that voted/liked that post. I cant quiet figure out how or even if it is possible. Any information would be very much appreciated.

Using different table name for 'votes'

Is it possible to use a different table name for 'votes'.

Actually I am working on an engine and want to use acts_as_votable for models in my engine, so ideally I would like to have the table name like 'myengine_votes' . I modified migration to use the different table name, but then the functions of this gem won't recognize the table...

Sorting post per vote?

Hello,
is it possible actually to retrieve the posts by voting order (the most upvoted first, the least voted last)?
Thanks

unliked_by and undisliked_by not working

Hello friends,
unliked_by and undisliked_by are not working after like or dislike operations.
Here is the console output for my project:

> @votable_model = Entry.find(10)
  Entry Load (53.0ms)  SELECT "entries".* FROM "entries" WHERE "entries"."id" = $1 LIMIT 1  [["id", 10]]
#<Entry id: 10, title: "Manowar", content: "Hail and kill", user_id: 2, created_at: "2012-10-13 14:06:11", updated_at: "2013-02-07 18:49:31", deleted: true, deleted_by: "moderator:test">

>> @user = User.find(2)
  User Load (1.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 2]]
#<User id: 2, email: "[email protected]", encrypted_password: "$2a$10$31Uba.OiixuTgvsiQgqB9uWQf8sKIwvt1Qgw7FhKI3Gc...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 109, current_sign_in_at: "2013-02-13 08:56:39", last_sign_in_at: "2013-02-12 17:47:24", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2012-07-14 16:28:18", updated_at: "2013-02-13 08:56:39", role: "moderator", username: "test", blocked: false>

>> @votable_model.liked_by @user
   (2.0ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = 10 AND "votes"."votable_type" = 'Entry' AND "votes"."voter_id" = 2 AND "votes"."vote_scope" IS NULL AND "votes"."voter_type" = 'User'
   (3.0ms)  BEGIN
  SQL (57.0ms)  INSERT INTO "votes" ("created_at", "updated_at", "votable_id", "votable_type", "vote_flag", "vote_scope", "voter_id", "voter_type") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"  [["created_at", Wed, 13 Feb 2013 13:53:40 UTC +00:00], ["updated_at", Wed, 13 Feb 2013 13:53:40 UTC +00:00], ["votable_id", 10], ["votable_type", "Entry"], ["vote_flag", true], ["vote_scope", nil], ["voter_id", 2], ["voter_type", "User"]]
   (6.0ms)  COMMIT
true

>> @votable_model.unliked_by @user
false

As seen above, first of all model is liked by the user, this action is saved to the vote table, and then model is unliked_by the same user but nothing happens. I mean record is not deleted from votes table.
Do you have any idea why this is broken, or am i missing something?

Thanks a lot.
//ร–mer

voted_on? in more detail

Is there a generic voted_on? function, instead of a voted_up_on? or voted_down_on?

Also, is it possible to check if a user voted on something for one or more scopes?
user.voted_on? @comment, votescope: 'funny'

a complete tutorial

hi..where I can find a complete tutorial for that gem ?..as I am new to ROR .regards

Voting without registration

Hello
In my application, there is no such thing as a "registration".
How can I make (using this plugin) for anonymous voting?

Not issue, just lack of knowledge

Hi, I am new at Rails and I am using act_as_votable with cached tables. Sorry for my ignorance, but, how do you count all the votes for all the answers?

I have tryed

  1. Answers.votes.up => "no method Answers".
  2. Answers(:cached_votes_up) => "same no method).
  3. ActAsVotable::Vote.all.size => "uninitialized constant ApplicationController::ActAsVotable".
    and more...

I know I can do:
@all_votes = 0
@all_votes = @answers.each do |answer|
@all_votes = @all_votes + answer.votes.up.size
end

But. if votes are already cached, why not use those tables? Problem is I do not manage to find correct phrase for @all_votes, @all_votes_up, @all_votes_down.

Thanks for the gem and for the answer.

Unknown column errors...

Hi,

Maybe i'm doing something wrong - but when call @model.liked_by current_user in my controller i get a "Unknown column 'votes.model_id' in 'where clause' error at runtime.

I tried adding an id column in the votes table which gets rid of the error - but then the votes don't register in the model... db gets updated with a new vote but no luck getting the values from the model.

Any pointers? This may well be an error on my part but it seems a bit fishy to me...

It looks to me like the query is getting built wrong (using model_id instead of votable_id in the where clause)

Vote Pagination

Any idea how to see all of the posts that the user has voted on? I am using will_paginate.

From what I could tell user.find_votes simply returns an array.

undefined method `likes' for

Hi.

After update to the latest version I've started facing this error:

Failure/Error: @post.likes.size.should eq(0)
NoMethodError:
undefined method `likes' for #Post:0x007fa95b88af98

Thank you!

Mutual exclusion

I wasn't sure where to post new features suggestions/questions. Please excuse me if I am polluting the issues board.

Currently scopes are independent. This is useful for votes like on yelp, where you can vote for "useful", "funny", "cool", and so on. But for creating a poll, mutual exclusion is necessary for a handful of specified scopes. Can this gem do this?

I am trying to use this gem as a polling system. In such, I will use a named scope for an option, but I want a given collection of named to be mutually exclusive. I envision the command to do this something along the lines of:

mutually_exclusive( 'collection_1', [ 'scope_1', 'scope_2', 'scope_3'], 2)

the array of scope names would be existing scopes that will become mutually exclusive, so that a voter cannot vote for more than one of those scopes.

Additionally, the default number of votes for a pool would be 1, but could be specified to be a greater integer). In the above example, a 3rd argument, an integer would specify this.

The first argument, 'collection_1' would be the new scope name representing the collection of these basic scopes. This would be useful when trying to make this scope mutually exclusive with other scopes. In other words, you would be able to take the string 'collection_1' and make that set of scopes mutually exclusive with other scopes (or scope sets) like 'collection_2' as well.

mutually_exclusive( 'collection_set_1', [ 'collection_1', 'collection_2', 'scope_4', 'scope_4'], 3)

As you can see, by using the same format for the scope collection as a plain old scope, it allows for easy chaining by being able to pass it into the same method.

I want to create a pull request myself, but I am not up to speed on the workings of this gem. I've looked through every file in this gem, but am not sure I understand everything. Also, it would be my first open source contribution. But if no one is planning to implement this, I am definitely going to try to create this myself. I would enjoy collaborating or getting some hints to accomplish mutual exclusion

I'd be glad to clarify anything that may have not been worded clearly.

NoMethodError: undefined method `liked_by' for #<Enumerator:0x00000004a72138>

I'm probably just doing something completely stupid as I'm a rails newb, but I followed to the best of my knowledge the documentation and get "NoMethodError: undefined method `liked_by' for #Enumerator:0x00000004a72138" error.

Added acts_as_votable to Gemfile
Ran: bundle install; rails generate acts_as_votable:migration; rake db:migrate
I've added "acts_as_voter" to my User model.
I've added "acts_as_votable" to my Post model.
All without any problems, but then when I try to do votes, it doesn't work:

1.9.3p194 :002 > @posts = Post.all
  Post Load (13.5ms)  SELECT "posts".* FROM "posts" 
 => [#<Post id: 1, ...>] 

1.9.3p194 :003 > @users = User.all
  User Load (5.7ms)  SELECT "users".* FROM "users" 
 => [#<User id: 1, ...>] 

1.9.3p194 :004 > @post = @posts.find(1)
 => #<Enumerator: [#<Post id: 1, ...>]:find(1)> 

1.9.3p194 :005 > @user = @users.find(1)
 => #<Enumerator: [#<User id: 1, ...>]:find(1)> 

1.9.3p194 :006 > @post.liked_by @user
NoMethodError: undefined method `liked_by' for #<Enumerator:0x00000004a72138>
    from (irb):6
    from /home/acase/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.5/lib/rails/commands/console.rb:47:in `start'
    from /home/acase/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.5/lib/rails/commands/console.rb:8:in `start'
    from /home/acase/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.5/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

1.9.3p194 :007 > @user.likes @post
NoMethodError: undefined method `likes' for #<Enumerator:0x000000055846c0>
    from (irb):7
    from /home/acase/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.5/lib/rails/commands/console.rb:47:in `start'
    from /home/acase/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.5/lib/rails/commands/console.rb:8:in `start'
    from /home/acase/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.5/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

Any ideas?

Vote's Scopes do not work

When I run this example in my rails console:

user = User.last
album = Album.last
album.liked_by user
reload!
user.votes.up.by_type(Album)

I get:

  ActsAsVotable::Vote Load (0.4ms)  SELECT "votes".* FROM "votes" WHERE (("votes"."voter_id" = 1 AND "votes"."voter_type" = 'User'))
NoMethodError: undefined method `up' for #<Array:0x007fa942feb148>

Same with: user.votes.down

unlike and undislike redundant?

Could there just one command to clear previously cast votes? What is the benefit of having both? If it's aliasing, it seems more like a downside than a benefit, due to the confusion and seemingly increased specificity when a single clear_vote or an alias of that would do.

breaking down votes by voter property?

I'm working on something where the user's ages would be good way of breaking down votes - ie item.cached_votes_up.where("user.age" => 3) or min/max...

How best to return groups of votes by voter prop?

Add ability to make votes unknown

The votes are unknown, but yet recorded. No trace of the type of the vote is kept, but a type counter is incremented for each vote.
That way, I do not know who voted what, but I know who voted.

Would be useful for security and law abiding purpose.

Finding votes within a date range

Consider the following scenario: I have a competition where at the end of each month I want to order and list Videos that have had the most likes. To find a list of users with the most votes for their videos, I can do:

leaderboard = Video.find_by_sql("SELECT sum(cached_votes_score) AS sum_score, user_id FROM videos GROUP BY user_id ORDER BY sum_score DESC")
=> [#<Video user_id: 44>, #<Video user_id: 11>] 

leaderboard[0].sum_score
=> 12

leaderboard[1].sum_score
=> 8

is it possible to do something similar, but on a date range? I guess I could run a rake task to get the above values on the last day of each month and store them in their own model/table logic.

Thanks for a great gem :)

unitialized constant [Class]::Votable?

Hey there - followed the directions, did the migrate, etc, added 'acts_as_votable' to my class (Episode) and am getting this in active admin:

Rendered /Users/grimm/.bundler/ruby/1.9.1/active_admin-a9949c152420/app/views/active_admin/resource/index.html.arb (2291.9ms)
Completed 500 Internal Server Error in 2663ms

NameError - uninitialized constant Episode::Votable:
  activerecord (4.0.0) lib/active_record/inheritance.rb:125:in `compute_type'
  activerecord (4.0.0) lib/active_record/reflection.rb:178:in `klass'
  /Users/grimm/.bundler/ruby/1.9.1/ransack-8db7a3c1f424/lib/ransack/adapters/active_record/context.rb:55:in `attribute_method?'
  /Users/grimm/.bundler/ruby/1.9.1/ransack-8db7a3c1f424/lib/ransack/nodes/grouping.rb:130:in `block in attribute_method?'
  /Users/grimm/.bundler/ruby/1.9.1/ransack-8db7a3c1f424/lib/ransack/nodes/grouping.rb:130:in `attribute_method?'
  /Users/grimm/.bundler/ruby/1.9.1/ransack-8db7a3c1f424/lib/ransack/search.rb:86:in `method_missing'
  actionpack (4.0.0) lib/action_view/helpers/tags/base.rb:28:in `value'
  actionpack (4.0.0) lib/action_view/helpers/tags/select.rb:15:in `block in render'
  actionpack (4.0.0) lib/action_view/helpers/tags/select.rb:15:in `render'
  actionpack (4.0.0) lib/action_view/helpers/form_options_helper.rb:151:in `select'
  actionpack (4.0.0) lib/action_view/helpers/form_options_helper.rb:769:in `select'
  /Users/grimm/.bundler/ruby/1.9.1/formtastic-cd6cb88f28ea/lib/formtastic/inputs/select_input.rb:154:in `select_html'
  /Users/grimm/.bundler/ruby/1.9.1/formtastic-cd6cb88f28ea/lib/formtastic/inputs/select_input.rb:149:in `block in to_html'
  actionpack (4.0.0) lib/action_view/helpers/capture_helper.rb:38:in `block in capture'
  actionpack (4.0.0) lib/action_view/helpers/capture_helper.rb:200:in `with_output_buffer'
  actionpack (4.0.0) lib/action_view/helpers/capture_helper.rb:38:in `capture'

An "after_vote" callback

In case you want to run a method after every vote.
I'm not an expert, I would guess "touching" the voted element would be the simplest way to do it? That way an after_save of the voted object would do

Thanks

unlike_by not working

Hi,

I can't seem to get unvoting to work. I have a Concepts model, which acts_as_votable, and a Hero (User) model that acts_as_voter. I'm not sure if there's anything wrong with my models:

Hero Model

(acts_as_voter)

class Hero < ActiveRecord::Base
  acts_as_voter
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :identity, :remember_me
  # attr_accessible :title, :body

  has_many :concepts
  has_many :comments
  has_many :implementations
end

Concept Model

(acts_as_votable)

class Concept < ActiveRecord::Base
    acts_as_votable
    acts_as_commentable
  attr_accessible :description, :name, :url

  has_many :implementations
  has_many :comments, as: :commentable, :dependent => :destroy
  belongs_to :hero
end

What I tried in rails console:

1.9.3-p194 :054 >   c = Concept.first
#  Concept Load (0.5ms)  SELECT "concepts".* FROM "concepts" LIMIT 1
# => #<Concept id: 1, name: "Test Implementation", url: nil, description: nil, created_at: "2012-11-14 23:47:29", updated_at: "2012-11-15 01:26:21", hero_id: 1, cached_votes_total: 1, cached_votes_up: 1, cached_votes_down: 0> 
1.9.3-p194 :055 > h = Hero.first
#  Hero Load (0.5ms)  SELECT "heroes".* FROM "heroes" LIMIT 1
# => #<Hero id: 1, email: "brian@*****.com", encrypted_password: "$2a$10$L29F4oMoQd0fyv6bkn34WeaO7dHGfEkuCgMQYqrzLoeC...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2012-11-14 23:47:28", updated_at: "2012-11-14 23:47:28", identity: "faitswulff"> 
1.9.3-p194 :056 > c.liked_by h
#   (0.5ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = 1 AND "votes"."votable_type" = 'Concept' AND "votes"."voter_id" = 1 AND "votes"."voter_type" = 'Hero'
#  ActsAsVotable::Vote Load (0.4ms)  SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = 1 AND "votes"."votable_type" = 'Concept' AND "votes"."voter_id" = 1 AND "votes"."voter_type" = 'Hero' LIMIT 1
#   (0.1ms)  begin transaction
#   (0.1ms)  commit transaction
#   (0.3ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = 1 AND "votes"."votable_type" = 'Concept'
#   (0.3ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = 1 AND "votes"."votable_type" = 'Concept' AND "votes"."vote_flag" = 't'
#   (0.3ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = 1 AND "votes"."votable_type" = 'Concept' AND "votes"."vote_flag" = 'f'
#   (0.1ms)  begin transaction
#   (0.1ms)  commit transaction
# => true 
1.9.3-p194 :057 > c.unlike_by h
NoMethodError: undefined method `unlike_by' for #<Concept:0x00000003ce2f90>
#        from /home/faitswulff/.rvm/gems/ruby-1.9.3-p194@collide/gems/activemodel-3.2.8/lib/active_model/attribute_methods.rb:407:in `method_missing'
#        from /home/faitswulff/.rvm/gems/ruby-1.9.3-p194@collide/gems/activerecord-3.2.8/lib/active_record/attribute_methods.rb:149:in `method_missing'
#        from (irb):57
#        from /home/faitswulff/.rvm/gems/ruby-1.9.3-p194@collide/gems/railties-3.2.8/lib/rails/commands/console.rb:47:in `start'
#        from /home/faitswulff/.rvm/gems/ruby-1.9.3-p194@collide/gems/railties-3.2.8/lib/rails/commands/console.rb:8:in `start'
#        from /home/faitswulff/.rvm/gems/ruby-1.9.3-p194@collide/gems/railties-3.2.8/lib/rails/commands.rb:41:in `<top (required)>'
#        from script/rails:6:in `require'
#        from script/rails:6:in `<main>'

Am I just doing something wrong?

Adding acts_as_votable to model cause activity records doubling when using public_activity gem

Hey,

I'm using acts_as_votable for my Song class so people can like/dislike songs, and also using public_activity to record when they add song to favourites.

Although I'm using different functions to up/down-vote and add to favourites a song and in add to favourites method I'm not using any code related with acts_as_votable gem.

The problem here is that if model has "acts_as_votable" the recorded activity is doubled.

Nonetheless, we are all coders, so code describe it best

Music::SongsController

def vote
    @song = Music::Song.find(params[:song_id])

    if current_user.voted_for? @song
      @song.unliked_by current_user
      render text: "unliked"
    else
      @song.liked_by current_user
      @song.create_activity :fav, owner: current_user 

      render text: "liked"
    end
  end

def favourite
    @song = Music::Song.find(params[:song_id])
    @song.create_activity :pin, owner: current_user 

    redirect_to current_user, notice: "Ok"
  end
class Music::Song < ActiveRecord::Base
  include Importable, Exportable
  include PublicActivity::Common

  # --- Acts ---
  acts_as_taggable
  #acts_as_votable           # Commented as this is the one causing problems

I guess it may have something in common with #83

Migration for upgrading

I upgraded from 0.5.0 to 0.9.0, so a migration was needed to add the vote_weight column to the vote model. Just wanted to drop this command here for anyone who is in the same boat:

rails generate migration add_vote_weight_to_votes vote_weight:integer

downvote_from does not work

The documentation in the readme cites downvote_from, but calling it results in an error. In votable.rb, I see upvote_from defined, but not downvote_from.

I guess either add the definition, or switch the docs to downvote_by.

Why voters can't vote more than once?

I'm dealing with a scenario that a user can vote more than once or maybe vote more than once with some time frame. I would like to know if there is some drawback of a voter voting more than once.

I'm trying to add a options[:duplicate] on vote to permit duplicates entries, but I was wondering if my tests is going in the right direction, since I'm only testing on' votable_spec.rb.

I really appreciate your answer. :)

update_cached_votes ignores vote_scope

* This one is fixed as it is not a bug. See my comment below*

My app runs with acts_as_votable v0.8.0 on Rails 4.1.0.rc1 and Ruby 2.1.1

The update_cached_votes method ignores vote_scope as you can see in the queries.

The queries used to get the data to update the cache columns always query for

AND "votes"."vote_scope" IS NULL

My Models are Users (voters, here: user1 and user2) and Feedbacks (votables, here: f1)

2.1.1 :015 > f1.liked_by user1
   (0.2ms)  SELECT COUNT(*) FROM "votes"  WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."voter_id" = 1 AND "votes"."vote_scope" IS NULL AND "votes"."voter_type" = 'User'  [["votable_id", 7], ["votable_type", "Feedback"]]
   (0.1ms)  begin transaction
  SQL (0.4ms)  INSERT INTO "votes" ("created_at", "updated_at", "votable_id", "votable_type", "vote_flag", "vote_weight", "voter_id", "voter_type") VALUES (?, ?, ?, ?, ?, ?, ?, ?)  [["created_at", "2014-03-15 15:04:46.804400"], ["updated_at", "2014-03-15 15:04:46.804400"], ["votable_id", 7], ["votable_type", "Feedback"], ["vote_flag", "t"], ["vote_weight", 0], ["voter_id", 1], ["voter_type", "User"]]
   (2.1ms)  commit transaction
   (0.2ms)  SELECT COUNT(*) FROM "votes"  WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_scope" IS NULL  [["votable_id", 7], ["votable_type", "Feedback"]]
   (0.1ms)  SELECT COUNT(*) FROM "votes"  WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = 't' AND "votes"."vote_scope" IS NULL  [["votable_id", 7], ["votable_type", "Feedback"]]
   (0.1ms)  SELECT COUNT(*) FROM "votes"  WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = 'f' AND "votes"."vote_scope" IS NULL  [["votable_id", 7], ["votable_type", "Feedback"]]
   (0.2ms)  SELECT SUM("votes"."vote_weight") AS sum_id FROM "votes"  WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = 't' AND "votes"."vote_scope" IS NULL  [["votable_id", 7], ["votable_type", "Feedback"]]
   (0.1ms)  SELECT SUM("votes"."vote_weight") AS sum_id FROM "votes"  WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = 'f' AND "votes"."vote_scope" IS NULL  [["votable_id", 7], ["votable_type", "Feedback"]]
   (0.0ms)  begin transaction
  SQL (0.5ms)  UPDATE "feedbacks" SET "cached_votes_score" = ?, "cached_votes_total" = ?, "cached_votes_up" = ?, "updated_at" = ? WHERE "feedbacks"."id" = 7  [["cached_votes_score", 1], ["cached_votes_total", 1], ["cached_votes_up", 1], ["updated_at", "2014-03-15 15:04:46.823143"]]
   (1.1ms)  commit transaction
 => true


2.1.1 :016 > f1.liked_by user2, vote_scope: "company_grade"
   (0.3ms)  SELECT COUNT(*) FROM "votes"  WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."voter_id" = 2 AND "votes"."vote_scope" = 'company_grade' AND "votes"."voter_type" = 'User'  [["votable_id", 7], ["votable_type", "Feedback"]]
   (0.1ms)  begin transaction
  SQL (0.3ms)  INSERT INTO "votes" ("created_at", "updated_at", "votable_id", "votable_type", "vote_flag", "vote_scope", "vote_weight", "voter_id", "voter_type") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)  [["created_at", "2014-03-15 15:05:15.842405"], ["updated_at", "2014-03-15 15:05:15.842405"], ["votable_id", 7], ["votable_type", "Feedback"], ["vote_flag", "t"], ["vote_scope", "company_grade"], ["vote_weight", 0], ["voter_id", 2], ["voter_type", "User"]]
   (2.3ms)  commit transaction
   (0.1ms)  SELECT COUNT(*) FROM "votes"  WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_scope" IS NULL  [["votable_id", 7], ["votable_type", "Feedback"]]
   (0.1ms)  SELECT COUNT(*) FROM "votes"  WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = 't' AND "votes"."vote_scope" IS NULL  [["votable_id", 7], ["votable_type", "Feedback"]]
   (0.1ms)  SELECT COUNT(*) FROM "votes"  WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = 'f' AND "votes"."vote_scope" IS NULL  [["votable_id", 7], ["votable_type", "Feedback"]]
   (0.1ms)  SELECT SUM("votes"."vote_weight") AS sum_id FROM "votes"  WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = 't' AND "votes"."vote_scope" IS NULL  [["votable_id", 7], ["votable_type", "Feedback"]]
   (0.1ms)  SELECT SUM("votes"."vote_weight") AS sum_id FROM "votes"  WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = 'f' AND "votes"."vote_scope" IS NULL  [["votable_id", 7], ["votable_type", "Feedback"]]
   (0.0ms)  begin transaction
   (0.0ms)  commit transaction
 => true


2.1.1 :017 > f1.liked_by user2
   (0.3ms)  SELECT COUNT(*) FROM "votes"  WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."voter_id" = 2 AND "votes"."vote_scope" IS NULL AND "votes"."voter_type" = 'User'  [["votable_id", 7], ["votable_type", "Feedback"]]
   (0.1ms)  begin transaction
  SQL (0.3ms)  INSERT INTO "votes" ("created_at", "updated_at", "votable_id", "votable_type", "vote_flag", "vote_weight", "voter_id", "voter_type") VALUES (?, ?, ?, ?, ?, ?, ?, ?)  [["created_at", "2014-03-15 15:05:34.465777"], ["updated_at", "2014-03-15 15:05:34.465777"], ["votable_id", 7], ["votable_type", "Feedback"], ["vote_flag", "t"], ["vote_weight", 0], ["voter_id", 2], ["voter_type", "User"]]
   (2.6ms)  commit transaction
   (0.1ms)  SELECT COUNT(*) FROM "votes"  WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_scope" IS NULL  [["votable_id", 7], ["votable_type", "Feedback"]]
   (0.1ms)  SELECT COUNT(*) FROM "votes"  WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = 't' AND "votes"."vote_scope" IS NULL  [["votable_id", 7], ["votable_type", "Feedback"]]
   (0.1ms)  SELECT COUNT(*) FROM "votes"  WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = 'f' AND "votes"."vote_scope" IS NULL  [["votable_id", 7], ["votable_type", "Feedback"]]
   (0.1ms)  SELECT SUM("votes"."vote_weight") AS sum_id FROM "votes"  WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = 't' AND "votes"."vote_scope" IS NULL  [["votable_id", 7], ["votable_type", "Feedback"]]
   (0.1ms)  SELECT SUM("votes"."vote_weight") AS sum_id FROM "votes"  WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = 'f' AND "votes"."vote_scope" IS NULL  [["votable_id", 7], ["votable_type", "Feedback"]]
   (0.0ms)  begin transaction
  SQL (0.2ms)  UPDATE "feedbacks" SET "cached_votes_score" = ?, "cached_votes_total" = ?, "cached_votes_up" = ?, "updated_at" = ? WHERE "feedbacks"."id" = 7  [["cached_votes_score", 2], ["cached_votes_total", 2], ["cached_votes_up", 2], ["updated_at", "2014-03-15 15:05:34.478671"]]
   (0.8ms)  commit transaction
 => true

I've also called the method directly

2.1.1 :018 > f1.update_cached_votes
   (0.2ms)  SELECT COUNT(*) FROM "votes"  WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_scope" IS NULL  [["votable_id", 7], ["votable_type", "Feedback"]]
   (0.1ms)  SELECT COUNT(*) FROM "votes"  WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = 't' AND "votes"."vote_scope" IS NULL  [["votable_id", 7], ["votable_type", "Feedback"]]
   (0.1ms)  SELECT COUNT(*) FROM "votes"  WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = 'f' AND "votes"."vote_scope" IS NULL  [["votable_id", 7], ["votable_type", "Feedback"]]
   (0.1ms)  SELECT SUM("votes"."vote_weight") AS sum_id FROM "votes"  WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = 't' AND "votes"."vote_scope" IS NULL  [["votable_id", 7], ["votable_type", "Feedback"]]
   (0.1ms)  SELECT SUM("votes"."vote_weight") AS sum_id FROM "votes"  WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = 'f' AND "votes"."vote_scope" IS NULL  [["votable_id", 7], ["votable_type", "Feedback"]]
   (0.1ms)  begin transaction
   (0.0ms)  commit transaction
 => true


2.1.1 :019 > f1.update_cached_votes "company_grade"
   (0.2ms)  SELECT COUNT(*) FROM "votes"  WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_scope" IS NULL  [["votable_id", 7], ["votable_type", "Feedback"]]
   (0.1ms)  SELECT COUNT(*) FROM "votes"  WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = 't' AND "votes"."vote_scope" IS NULL  [["votable_id", 7], ["votable_type", "Feedback"]]
   (0.1ms)  SELECT COUNT(*) FROM "votes"  WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = 'f' AND "votes"."vote_scope" IS NULL  [["votable_id", 7], ["votable_type", "Feedback"]]
   (0.1ms)  SELECT SUM("votes"."vote_weight") AS sum_id FROM "votes"  WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = 't' AND "votes"."vote_scope" IS NULL  [["votable_id", 7], ["votable_type", "Feedback"]]
   (0.1ms)  SELECT SUM("votes"."vote_weight") AS sum_id FROM "votes"  WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = 'f' AND "votes"."vote_scope" IS NULL  [["votable_id", 7], ["votable_type", "Feedback"]]
   (0.1ms)  begin transaction
   (0.0ms)  commit transaction
 => true

Model Access for After_Create Access

Hi hello,

This isn't really an issue more of question. Is there a way to access the model for the acts_as_votable? I'm trying to once the data is saved to my postgres database then save it to a Redis DB for quick access for a feed I'm working on.

Thank you!

access to votes by other users

I am trying to make a reputation system where you can find out how many times users have voted on another person's object. I only seem to be able to access an individual user's votes. It seems incredibly inefficient to loop through all the user's to find out who voted for the current user's object. Any suggestions?

problem when votable is a child class of a AR class

I have:

class Post
...
end

class Message<Post
acts_as_votable
end

now when I do:

m=Message.first
m.vote :voter=>User.first
m.votes returns []
and this is because the select is looking for votable_type 'Message' not 'Post'

What do you think?
-Matteo

One voted for STI'ed model and called `liked?` on it will always return `false

I have a STI Post model that two sub classes that inherit from the Post model,

class Article < Post
end

and

class Evidence < Post
end

I have placed acts_as_votable inside the Post class

class Post < ActiveRecord::Base
  acts_as_votable
end

I have an User model that has acts_as_voter inside the User class

When I call user.likes article, then I call user.liked? article, I will get a false
When a vote is created, the votable_type is Post but not Article

This PR #77 should have fixed the issue.

Any idea?

Model can't be both acts_as_votable and acts_as_voter

If you add both these helpers in one model, for example:

class User < ActiveRecord::Base
  acts_as_voter
  acts_as_votable
end

Some of the method/scopes on the model will be broken, as voter and votable has many methods with the same names, but absolutely different behaviour. What will be broken is dependent from the helpers order:

  1. If acts_as_voter comes first, user.votes will return not scope, but a boolean. The voting itself is working.
  2. If acts_as_votable goes first, it seems like all methods (other_votable.vote_from user or votable.votes for example) won't work anymore, complaining that there is no vote method for nil.

It possibly means, that it will be required to change gem API to solve this (rename some methods either for voter or votable).

Any suggestions, may be I can help? I know ruby enough to make changes in gems, but I'm little afraid :-)

Rename the "votes" table

Hi,

I already have a table called "votes" in my database, and I would want to know if there is a way to rename it and make the gem using this new table. Is there a way to configure the gem like that ?

Because when I try to "rake db:migrate", I've a pretty exception because a "votes" table already exists. And I'm afraid that renaming directly the gem's table in the migration will not work, isn't it ?

License?

Please include a license with this code. I'd like to use it if its an MIT license.

All votes a user has made

Have you a plan of when and how to implement the @user.likes (which returns all items a user has voted on) feature or is it all ready implemented?

I need it so might implement it and put in a pull request.

voter and votable

It appears it's not possible to have a model be both votable and a voter. Is this true?

If my model is:

class User < ActiveRecord::Base
  acts_as_voter
  acts_as_votable
end

Then, when I try to use current_user.voted_up_on?(resource), the query I see is the following and always returns no results:

SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = 13746 AND "votes"."votable_type" = 'User' AND "votes"."votable_id" = 180 AND "votes"."votable_type" = 'Resource' AND "votes"."vote_scope" IS NULL AND "votes"."vote_flag" = 't'

I'm going to define my own voted_up_on? method in the meantime, and I'll remove acts_as_voter, but I figured this was worth mentioning in case I'm missing anything.

update table

Hello
I just updated the gem after a year and got an error, while db changed over time
NoMethodError (undefined method `vote_weight=' for # ...
After manually adding col everything looks fine
t.integer :vote_weight
br G

caching not working as expected

I ran the migration as shown in the readme, but whenever I do object.get_likes.size it still queries the votes table without hitting the cache. Additionally the cache columns are not automatically updating with new votes. Are there any procedures to perform?

Score: <%[email protected]_likes.size - @card.get_dislikes.size%>
Cached Score: <%[email protected]_votes_up.size - @card.cached_votes_down.size%>

Using without Devise

I'm wondering if it's possible to use this without having users on my App? I have tested a few things but can't seem to get it working...

Rails 4 Can't mass-assign attribute error

I have a model such as

class Video < ActiveRecord::Base
    acts_as_votable
        ...
end

And a spec such as

    it "should allow users to like a video" do
        @new_user = FactoryGirl.create(:user)
        @video = FactoryGirl.create(:video)
        @video.liked_by @new_user
        @video.votes.size.should == 1
    end

When running this, I get an error on console:

WARNING: Can't mass-assign protected attributes for ActsAsVotable::Vote: votable, voter, vote_scope

Any tips?

NoMethodError: undefined method 'where' in votable:154

I've setup exactly as described, and am going nuts with this error.

I have a questions AR model which has 'acts_as_votable".

I have a user AR mode which has 'acts_as_voter'.

irc$ q = Question.find(params[:id])
irc$ u = current_user

When I do

irc$ q.vote :voter => u
2012-11-07 13:51:56 +0530 (39330)   Question Load (5.5ms)  SELECT "questions".* FROM "questions" WHERE "questions"."id" = $1 LIMIT 1  [["id", 30]]
2012-11-07 13:51:56 +0530 (39330)   User Load (0.9ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 3]]
NoMethodError: undefined method `where' for 5:Fixnum
    from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/acts_as_votable-0.4.0/lib/acts_as_votable/votable.rb:154:in `find_votes'
    from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/acts_as_votable-0.4.0/lib/acts_as_votable/votable.rb:83:in `vote'
    from (irb):3
    from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.0/lib/rails/commands/console.rb:47:in `start'
    from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.0/lib/rails/commands/console.rb:8:in `start'
    from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.0/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

Any idea on what I'm doing wrong? The 'votes' table is empty, and adding votes via console or web fails

undefined method vote_weight

I am getting following error on version 0.8.0

MethodError (undefined method `vote_weight=' for #ActsAsVotable::Vote:0x00000008f70488)

on

@comment.liked_by @user

But same thing is working in 0.7.1

Any specific change do I need to do?

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.