medihack/make_voteable

up_voted? does not work for STI instances.

Opened this issue · 1 comments

class Post < ActiveRecord::Base
attr_accessible :type
end

class Link < Post
end

irb(main):012:0> User.first.up_vote! Link.first
INSERT INTO votings (created_at, up_vote, updated_at, voteable_id, voteable_type, voter_id, voter_type) VALUES ('2012-10-14 07:15:41', 1, '2012-10-14 07:15:41', 62, 'Post', 1, 'User')

irb(main):013:0> User.first.up_voted? Link.first
MakeVoteable::Voting Load (0.3ms) SELECT votings.* FROM votings WHERE votings.voteable_type = 'Link' AND votings.voteable_id = 62 AND votings.voter_type = 'User' AND votings.voter_id = 1 LIMIT 1
=> false

Following code fixes the problem. You can simply add this to your voter model. (In my case, it's app/modes/User.rb)

def fetch_voting(voteable)
MakeVoteable::Voting.where(
:voteable_type => voteable.class.base_class.to_s,
:voteable_id => voteable.id,
:voter_type => self.class.to_s,
:voter_id => self.id).try(:first)
end