0
0
Ruby on Railsframework~10 mins

Serializers (Active Model Serializers) in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Serializers (Active Model Serializers)
Controller receives request
Fetch model data from DB
Pass model to Serializer
Serializer formats data
Render JSON response
Client receives formatted JSON
The controller gets data, sends it to the serializer, which formats it into JSON, then sends it back to the client.
Execution Sample
Ruby on Rails
class UserSerializer < ActiveModel::Serializer
  attributes :id, :name, :email
end
Defines a serializer that formats User objects to include only id, name, and email in JSON.
Execution Table
StepActionInput DataSerializer OutputNotes
1Controller fetches User with id=1User(id=1, name='Alice', email='alice@example.com', password='secret')N/ARaw model data fetched from DB
2Pass User to UserSerializerUser objectUserSerializer instance createdSerializer prepares to format data
3Serializer reads attributesUser object{"id":1, "name":"Alice", "email":"alice@example.com"}Only specified attributes included
4Render JSON responseSerialized JSON{"id":1, "name":"Alice", "email":"alice@example.com"}Client-ready JSON sent
5Client receives JSONJSON stringParsed JSON objectClient can use data easily
💡 Serialization completes after JSON is rendered and sent to client
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
usernilUser(id=1, name='Alice', email='alice@example.com', password='secret')Same User objectSame User objectSame User object
serialized_jsonnilnilnil{"id":1, "name":"Alice", "email":"alice@example.com"}{"id":1, "name":"Alice", "email":"alice@example.com"}
Key Moments - 2 Insights
Why does the password not appear in the JSON output?
Because the serializer only includes attributes explicitly listed (:id, :name, :email), so password is excluded as shown in step 3 of the execution_table.
What happens if the model has nil values for attributes?
The serializer includes the attribute with a null value in JSON, since it serializes the attribute as is, visible in step 3 if any attribute is nil.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what attributes are included in the serialized JSON?
Aname, email
Bid, name, email
Cid, name, email, password
DAll user attributes
💡 Hint
Check the 'Serializer Output' column at step 3 in the execution_table
At which step does the serializer create the JSON string?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Serializer Output' column where JSON string first appears
If you add :created_at to attributes in the serializer, what changes in variable_tracker after step 3?
Aserialized_json becomes nil
Buser variable changes to include created_at
Cserialized_json includes created_at attribute
DNo change in serialized_json
💡 Hint
Adding attributes affects serialized_json output, check variable_tracker's serialized_json row
Concept Snapshot
Active Model Serializers format Rails models into JSON.
Define attributes in serializer class.
Controller passes model to serializer.
Serializer outputs JSON with only listed attributes.
Excludes sensitive data by omission.
Used to cleanly shape API responses.
Full Transcript
In Rails, Active Model Serializers help convert model objects into JSON format for APIs. The controller fetches data from the database, then passes the model instance to the serializer. The serializer reads only the attributes you list and creates a JSON string with those values. This JSON is then sent back to the client. For example, if you have a User model with many fields, but only want to expose id, name, and email, you list those in the serializer. Passwords or other sensitive data are not included unless explicitly added. This process ensures your API responses are clean and safe. The execution table shows each step: fetching the user, creating the serializer, generating JSON, and sending it to the client. Variables like the user object stay the same, but the serialized_json variable changes from nil to the JSON string after serialization. This helps beginners see exactly how data flows and changes during serialization.