1

Most of the questions on SO seems to be too outdated to solve this problem.

I want to serialize a model. Here's what my serializer looks like -

class AssignmentSerializer < ActiveModel::Serializer
  belongs_to :lesson, class_name: "Lesson"
  attributes :id, :student_id, :tutor, :name, :start_date, :end_date, :description, :lesson
end

This works perfectly well for situations where you want to serialize a single object in this form.

def index
    if current_user&.student?
        @assignments = Assignment.where(student_id: current_user.id)
        @assignments_due = Assignment.find_due(current_user)
        @submitted_assignments = Assignment.find_submitted(current_user)
    elsif current_user&.tutor?
        @assignments = Assignment.where(tutor_id: current_user.id)
    end
    respond_to do |format|
        format.json { render json: @assignments }
    end
end

But doesn't work when I want to serialize multiple objects like so:

def index
    if current_user&.student?
        @assignments = Assignment.where(student_id: current_user.id)
        @assignments_due = Assignment.find_due(current_user)
        @submitted_assignments = Assignment.find_submitted(current_user)
    elsif current_user&.tutor?
        @assignments = Assignment.where(tutor_id: current_user.id)
    end
    respond_to do |format|
        format.json { render json: {
            assignments: @assignments,
            assignments_due: @assignments_due,
            submitted_assignments: @submitted_assignments
            }, each_serializer: AssignmentSerializer
        }
    end
end

I have tried multiple methods by following different methods I saw in this documentation but none seems to work.

Any idea what I could be doing wrong?

Update tried as suggested in answer and comment, but this approach did not work. Tried with and without the each_serializer key

respond_to do |format|
    format.json { render json: {
        assignments: @assignments.as_json,
        assignments_due: @assignments_due.as_json,
        submitted_assignments: @submitted_assignments.as_json
        }, each_serializer: AssignmentSerializer
    }
end
mayorsanmayor
  • 2,870
  • 4
  • 24
  • 44

2 Answers2

3

Apparently, the answer was right there all along, it was just a bit confusing. In the same link I shared I found the answer. This link

So this is how I did it, using ActiveModelSerializers::SerializableResource.new

def index
    if current_user&.student?
        @assignments = Assignment.where(student_id: current_user.id)
        @assignments_due = Assignment.find_due(current_user)
        @submitted_assignments = Assignment.find_submitted(current_user)
    elsif current_user&.tutor?
        @assignments = Assignment.where(tutor_id: current_user.id)
    end
    respond_to do |format|
        format.json { render json: {
            assignments: ActiveModelSerializers::SerializableResource.new(@assignments),
            assignments_due: ActiveModelSerializers::SerializableResource.new(@assignments_due),
            submitted_assignments: ActiveModelSerializers::SerializableResource.new(@submitted_assignments)
            }
        }
    end
end

That gave me the exact structure that I wanted.

mayorsanmayor
  • 2,870
  • 4
  • 24
  • 44
  • I was posting a solution like you just found, based on this topic: https://stackoverflow.com/questions/36278413/serialize-an-array-of-models-using-active-model-serializers – iGian Sep 02 '18 at 12:01
  • @iGian thank you very much. I didn't find this link while I was searching. – mayorsanmayor Sep 02 '18 at 12:05
0

As per: https://github.com/rails/rails/blob/master/activemodel/lib/active_model/serializers/json.rb#L89

@assignments.as_json should serialize your assignments using AssignmentSerializer as you want to achieve.

Tarek N. Elsamni
  • 1,718
  • 1
  • 12
  • 15