我想检查登录用户是否为特定帖子投票。我想向 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>
我认为问题在于当前用户没有被单个帖子的范围所看到。我该如何解决这个问题?