检查用户是否已使用范围投票

我使用行为投票来实施网络民意调查。 通过两种选择,可以很容易地确定用户是否已投票。

@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 

https://github.com/ryanto/acts_as_votable

我需要定制多个选项并基于此实现它: 如何使用act-as-votable设置多选项投票系统?

上面的项目说明您可以检查用户是否已使用voted_for投票? 但是这包括范围项目:

 Poll.first.vote_by voter: User.first, vote_scope: 'blue' User.first.voted_for? Poll.first #false User.first.voted_for? Poll.first, :vote_scope => 'blue' #true 

我的问题是,确定用户在使用范围时是否投票的最佳方法是什么? 我是否需要循环检查每个记录的每个范围?

编辑1

目前我有以下Poll实例方法:

 def has_voted?(user) ['red', 'green', 'blue', 'white'].each do |option| if user.voted_for? self, :vote_scope => option return true end end return false end Poll.first.has_voted?(User.first) 

看起来您应该能够调用Poll.first.votes_for并获得已投票的投票列表:

 p.votes_for => #]> 

使用该列表,您应该能够检查是否有任何voter_ids与您正在寻找的User匹配:

 p.votes_for.any? { |v| v.voter_id == u.id } => true