Reputation: 45
I'm having some trouble to migrate from jbuilder to active_model_serializers (AMS v0.9). To construct the previus json I need an other object to be passed as argument into my serializer.
I've seen that there are some options for that, but nothing is working for me.
Here is my controller where I call the serializer:
def show
serialized_profile = ProfileSerializer.new(@profile, root: false,
scope: current_relationship,
couple_ser: @couple,
partners_ser: @partners)
respond_to do |format|
format.html
format.json { render json: serialized_profile }
end
end
The @profile
, @couple
and @partner
are set in a before_action
.
And here is my serializer:
class ProfileSerializer < ActiveModel::Serializer
attributes :couple
def couple
return nil if serialization_options[:couple_ser].nil?
serialization_options[:couple_ser]
end
end
The thing is that I am totally sure that my controller has the variable @couple
when request the show method. However its always throwing null.
I am relatively new in ruby on rails. So could someone help me out?
Just to be aware, I posted only the essential parts of the code to be discussed.
Cheers
Upvotes: 1
Views: 2016
Reputation: 632
For v0.9
Have you tried something like this?
ProfileSerializer
.new(@profile, {root: false, scope: current_relationship})
.as_json({couple_ser: @couple, partners_ser: @partners})
Upvotes: 3
Reputation: 301
If you are using AMS v0.10 you should use '@instance_options' instead of 'serialization_options' in your serializer.
Upvotes: 1