ryanto / acts_as_votable

Votable ActiveRecord for Rails
1.53k stars 217 forks source link

unlike_by not working #20

Closed ghost closed 11 years ago

ghost commented 11 years ago

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?

ryanto commented 11 years ago

Thanks, thats a documentation bug. The correct method is "disliked_by".

ghost commented 11 years ago

Ah, thank you for the clarification. What's the proper way to remove a vote, then?

ryanto commented 11 years ago

Sorry "disliked_by" casts a negative vote.

"unliked_by" will remove the vote.

I will update the docs!

ghost commented 11 years ago

Sorry to bother you, but I also tried unliked_by and all it seems to do is return false :(

1.9.3-p194 :073 >   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 :074 > c.votes.up.count
   (0.5ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = 1 AND "votes"."votable_type" = 'Concept' AND "votes"."vote_flag" = 't'
 => 1 
1.9.3-p194 :075 > c.unliked_by h
 => false 
1.9.3-p194 :076 > c.votes.up.count
   (0.5ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = 1 AND "votes"."votable_type" = 'Concept' AND "votes"."vote_flag" = 't'
 => 1
ryanto commented 11 years ago

I know the guy that added this feature wrote a test for it. Let me checkout and run the tests to see whats going on.

ryanto commented 11 years ago

Ok, again documentation is wrong. Need to pass keyword hash into unvote function

c.unliked_by :voter => h
ghost commented 11 years ago

That did it. Thank you for the swift reply!

parruda commented 10 years ago

Hey guys, I am trying this gem now and it seems to not be working. When I try "post.unliked_by user" after liking it, it just returns true and does not delete the vote. I tried "post.unliked_by voter: user" and it returns: NoMethodError: undefined method `id' for #Hash:0x007ffb1929d6e8