Hello AMSers!
I need some help and I hope I have come to the right place.
I am using AMS 0.8.3 and I am trying to use a conditional in a serializer to specify which serializer to use for a certain association. The standard json end point has a list of users with basic information. Then I want to have an "export" end point where more detailed user information would be output into a CSV or something like that.
Here's what I have so far...
class PrizeController < ApplicationController
def entrants
render json: @prize, export: true, serializer: PrizeEntrantsSerializer
end
I'm passing in the export: true option thinking that I can use that in the conditional.
class PrizeEntrantsSerializer < ActiveModel::Serializer
attributes :id, :event, :event_prize_type,
has_many :entrants, serializer: which_serializer
def which_serializer
@options[:export] == true ? PrizeEntrantExportSerializer : PrizeEntrantSerializer
end
end
That didn't work. I get undefined local variable or method 'which_serializer', so I tried this:
class PrizeEntrantsSerializer < ActiveModel::Serializer
attributes :id, :event, :event_prize_type
if @options[:export] == true
has_many :entrants, serializer: PrizeEntrantExportSerializer
else
has_many :entrants, serializer: PrizeEntrantSerializer
end
end
That doesn't work either. I get undefined method '[]' for Nil Class. I am able to pass in the @options successfully because I tested it out by adding as an attribute and it does return correctly. Is it just not possible to have conditionals in a serializer?
Thanks for your help!
Hello AMSers!
I need some help and I hope I have come to the right place.
I am using AMS 0.8.3 and I am trying to use a conditional in a serializer to specify which serializer to use for a certain association. The standard json end point has a list of users with basic information. Then I want to have an "export" end point where more detailed user information would be output into a CSV or something like that.
Here's what I have so far...
I'm passing in the
export: trueoption thinking that I can use that in the conditional.That didn't work. I get
undefined local variable or method 'which_serializer', so I tried this:That doesn't work either. I get
undefined method '[]' for Nil Class. I am able to pass in the@optionssuccessfully because I tested it out by adding as an attribute and it does return correctly. Is it just not possible to have conditionals in a serializer?Thanks for your help!