0
0
Ruby on Railsframework~20 mins

Serializers (Active Model Serializers) in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Active Model Serializers Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What JSON output does this serializer produce?
Given the following serializer and model instance, what is the JSON output?
Ruby on Rails
class UserSerializer < ActiveModel::Serializer
  attributes :id, :name
  has_many :posts
end

user = User.new(id: 1, name: 'Alice')
post1 = Post.new(id: 10, title: 'Hello')
post2 = Post.new(id: 11, title: 'World')
user.posts = [post1, post2]

serialized = ActiveModelSerializers::SerializableResource.new(user).to_json
A{"user":{"id":1,"name":"Alice"}}
B{"id":1,"name":"Alice"}
C{"id":1,"name":"Alice","posts":[{"id":10,"title":"Hello"},{"id":11,"title":"World"}]}
D{"user":{"id":1,"name":"Alice","posts":[{"id":10,"title":"Hello"},{"id":11,"title":"World"}]}}
Attempts:
2 left
💡 Hint
Active Model Serializers wraps the output in a root key by default.
📝 Syntax
intermediate
1:30remaining
Which serializer code snippet is syntactically correct?
Identify the correct syntax for defining attributes and associations in an Active Model Serializer.
A
class ArticleSerializer &lt; ActiveModel::Serializer
  attributes :id, :title
  has_one :author
end
B
class ArticleSerializer &lt; ActiveModel::Serializer
  attribute :id, :title
  has_one :author
end
C
class ArticleSerializer &lt; ActiveModel::Serializer
  attributes :id :title
  has_one :author
end
D
class ArticleSerializer &lt; ActiveModel::Serializer
  attributes :id, :title
  has_one author
end
Attempts:
2 left
💡 Hint
Check the method names and syntax for attributes and associations.
🔧 Debug
advanced
2:30remaining
Why does this serializer raise an error?
Given this serializer code, why does it raise a NoMethodError when serializing?
Ruby on Rails
class CommentSerializer < ActiveModel::Serializer
  attributes :id, :content, :user_name

  def user_name
    object.user.name
  end
end

comment = Comment.new(id: 5, content: 'Nice post!')
serialized = ActiveModelSerializers::SerializableResource.new(comment).to_json
AThe method 'user_name' is not defined correctly; it should be a class method.
BThe comment object has no associated user, so 'object.user' is nil causing NoMethodError on 'name'.
CThe 'attributes' method cannot include custom methods like 'user_name'.
DThe serializer is missing 'has_one :user' declaration causing the error.
Attempts:
2 left
💡 Hint
Check if the associated user object exists before calling its methods.
state_output
advanced
2:00remaining
What is the value of 'serialized' after running this code?
Consider this serializer with conditional attribute inclusion. What JSON does it produce when 'include_email' is false?
Ruby on Rails
class UserSerializer < ActiveModel::Serializer
  attributes :id, :name
  attribute :email, if: :include_email?

  def include_email?
    instance_options[:include_email] == true
  end
end

user = User.new(id: 2, name: 'Bob', email: 'bob@example.com')
serialized = ActiveModelSerializers::SerializableResource.new(user, include_email: false).to_json
A{"user":{"id":2,"name":"Bob","email":"bob@example.com"}}
B{"id":2,"name":"Bob"}
C{"user":{"id":2,"name":"Bob"}}
D{"id":2,"name":"Bob","email":"bob@example.com"}
Attempts:
2 left
💡 Hint
The email attribute is included only if 'include_email' option is true.
🧠 Conceptual
expert
3:00remaining
Which statement about Active Model Serializers caching is true?
Select the correct statement about how caching works in Active Model Serializers.
ACaching requires manual cache key management and does not expire automatically when object attributes change.
BCaching stores the entire serialized JSON output per object and automatically expires when the object changes.
CCaching is enabled by default and uses Rails fragment caching with no configuration needed.
DCaching only works for has_many associations, not for individual objects.
Attempts:
2 left
💡 Hint
Think about how cache keys and expiration are handled in serializers.