【发布时间】:2017-01-12 17:44:59
【问题描述】:
给定以下控制器代码:
def show
render json: @post, include: 'author', adapter: :json_api
end
在序列化器中我可以访问include 指令吗?
class PostSerializer < ActiveModel::Serializer
attributes :title, :body, :foo
belongs_to :author
def foo
# can I gain access to the include_directive here somehow?
end
end
我查看了 @attributes、@instance_options、@object、@root @scope 和 @serializer_class(我可以通过 pry 看到的所有实例变量),但没有运气。
我正在使用active_model_serializers (0.10.2)。
【问题讨论】:
-
是的,你可以使用
@options[:include]。 -
@PriyankGupta 在
0.10之前可能可用吗?我在我的序列化程序中查看@options,它是nil。 -
试试看,
json: { post: @post, include: 'author', adapter: :json_api } -
@PriyankGupta,不幸的是,以这种方式实现的代码从未实例化序列化程序。我尝试在使用这种格式时也指定序列化程序类,但那里也没有骰子。
标签: ruby-on-rails active-model-serializers