如何使用葡萄实体将参数传递给模型方法?
我想在展示物品时检查current_user是否喜欢物品,所以我建立了一个模型user_likes?
方法:
class Item
include Mongoid::Document
#some attributes here...
has_and_belongs_to_many :likers
def user_likes?(user)
likers.include?(user)
end
end
但我不知道如何将 current_user 发送到葡萄实体模型:
module FancyApp
module Entities
class Item < Grape::Entity
expose :name #easy
expose :user_likes # <= How can I send an argument to this guy ?
end
end
end
在葡萄 api 中:
get :id do
item = Item.find(.....)
present item, with: FancyApp::Entities::Item # I should probably send current_user here, but how ?
end
我觉得 current_user 可能应该从最后一段代码发送,但我不知道该怎么做:(
有什么想法吗 ?谢谢 !