Giter Club home page Giter Club logo

acts_as_votable's Issues

License?

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

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

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

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.

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

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?

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 :-)

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.

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!

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

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%>

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 :)

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

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'

a complete tutorial

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

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.

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

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.

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?

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

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.

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?

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!

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.

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. :)

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 ?

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'

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

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?

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?

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

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...

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.

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...

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?

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?

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

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.

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)

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?

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.

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.