我想检查登录用户是否已为特定帖子投了一票。我想在Post的JSON属性中添加一个属性来执行此操作。以下是Post模型的相关代码:
def user_has_voted
if current_user?
self.voted_by(current_user)
end
end
def self.public_models(posts)
posts.to_json({:include => {:user => { :only => [:uid, :email] }, :company => { :only => :name} }, :methods => [:image_url, :total_votes, :user_has_voted]}).html_safe
end
user_has_voted方法存在问题,因为我收到以下错误:
undefined local variable or method `current_user' for #<Post:0x00000004d8eab0>
我认为问题在于当前用户不会被个别Post的范围看到。我怎么能解决这个问题?