ActiveModel Serializer - Passing params to serializers

两盒软妹~` 提交于 2019-12-10 05:02:43

问题


AMS version: 0.9.7

I am trying to pass a parameter to an ActiveModel serializer without any luck.

My (condensed) controller:

class V1::WatchlistsController < ApplicationController

   def index
     currency = params[:currency]
     @watchlists = Watchlist.belongs_to_user(current_user)
     render json: @watchlists, each_serializer: WatchlistOnlySerializer
   end

My serializer:

class V1::WatchlistOnlySerializer < ActiveModel::Serializer
  attributes :id, :name, :created_at, :market_value
  attributes :id


  def filter(keys)
    keys = {} if object.active == false 
    keys
  end 

  private

  def market_value
    # this is where I'm trying to pass the parameter
    currency = "usd"
    Balance.watchlist_market_value(self.id, currency)
  end

I am trying to pass a parameter currency from the controller to the serializer to be used in the market_value method (which in the example is hard-coded as "usd".

I've tried @options and @instance_options but I cant seem to get it work. Not sure if its just a syntax issue.


回答1:


AMS version: 0.10.6

Any options passed to render that are not reserved for the adapter are available in the serializer as instance_options.

In your controller:

def index
  @watchlists = Watchlist.belongs_to_user(current_user)
  render json: @watchlists, each_serializer: WatchlistOnlySerializer, currency: params[:currency]
end

Then you can access it in the serializer like so:

def market_value
  # this is where I'm trying to pass the parameter
  Balance.watchlist_market_value(self.id, instance_options[:currency])
end

Doc: Passing Arbitrary Options To A Serializer


AMS version: 0.9.7

Unfortunately for this version of AMS, there is no clear way of sending parameters to the serializer. But you can hack this using any of the keywords like :scope (as Jagdeep said) or :context out of the following accessors:

attr_accessor :object, :scope, :root, :meta_key, :meta, :key_format, :context, :polymorphic

Though I would prefer :context over :scope for the purpose of this question like so:

In your controller:

def index
  @watchlists = Watchlist.belongs_to_user(current_user)
  render json: @watchlists,
    each_serializer: WatchlistOnlySerializer,
    context: { currency: params[:currency] }
end

Then you can access it in the serializer like so:

def market_value
  # this is where I'm trying to pass the parameter
  Balance.watchlist_market_value(self.id, context[:currency])
end



回答2:


Try using scope in controller:

def index
 @watchlists = Watchlist.belongs_to_user(current_user)
 render json: @watchlists, each_serializer: WatchlistOnlySerializer, scope: { currency: params[:currency] }
end

And in your serializer:

def market_value
  Balance.watchlist_market_value(self.id, scope[:currency])
end



回答3:


You can send your params to your serializer like this

render json: @watchlists, each_serializer: WatchlistOnlySerializer, current_params: currency

and in your serializer you can use this to get the value

serialization_options[:current_params]


来源:https://stackoverflow.com/questions/47565916/activemodel-serializer-passing-params-to-serializers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!