class QuestionSerializer < ActiveModel::Serializer
attributes :id,:body,:votes,:liked
def body
object.body
end
def liked
if current_user.liked? object
true
else
false
end
end
def votes
object.votes.up.count
end
end
I have this method in the ApplicationController.
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
helper_method :current_user
That could not happen because QuestionSerializer didnt inherit from ApplicationController. What I did was then in Question model class defined this method which had access to the current_user.
def liked?
if self.liked_by? current_user
true
else
false
end
end