ruby - Array in serializer with multiple serializer rails -
i getting array after processing.
gem 'active_model_serializers'
now want use serializer
send data in json format.
but when called serializer, it'll not getting object because of array.
controller -
def index question = user.questions.available answer = user.where(:answer => params[:ans]) render :json => {:qust => question, :each_serializer => questionserializer, :ans => answer, :each_serializer => answerserializer} end
question_serializer.rb
class questionserializer < activemodel::serializer attributes :id, :question, :type end
answer_serializer.rb
class answerserializer < activemodel::serializer attributes :id, :answer, :date def date object.date = "..." end end
try this:
render json: { qust: activemodel::arrayserializer.new(question, each_serializer: questionserializer), ans: activemodel::arrayserializer.new(answer, each_serializer: answerserializer), }
side remarks:
- beware of naming, use plural when work collections.
answer = user.where(:answer => params[:ans])
looks bad
Comments
Post a Comment