Challenge - 5 Problems
Active Model Serializers Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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
Attempts:
2 left
💡 Hint
Active Model Serializers wraps the output in a root key by default.
✗ Incorrect
By default, Active Model Serializers wraps the serialized object in a root key named after the model (here 'user'). The attributes and associations appear inside this root key.
📝 Syntax
intermediate1:30remaining
Which serializer code snippet is syntactically correct?
Identify the correct syntax for defining attributes and associations in an Active Model Serializer.
Attempts:
2 left
💡 Hint
Check the method names and syntax for attributes and associations.
✗ Incorrect
The method to declare multiple attributes is 'attributes' with symbols separated by commas. Associations use 'has_one' or 'has_many' with a symbol argument.
🔧 Debug
advanced2: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
Attempts:
2 left
💡 Hint
Check if the associated user object exists before calling its methods.
✗ Incorrect
The error happens because 'object.user' is nil (no user associated), so calling 'name' on nil raises NoMethodError.
❓ state_output
advanced2: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
Attempts:
2 left
💡 Hint
The email attribute is included only if 'include_email' option is true.
✗ Incorrect
Since 'include_email' is false, the 'email' attribute is excluded from the output. The root key 'user' wraps the attributes.
🧠 Conceptual
expert3:00remaining
Which statement about Active Model Serializers caching is true?
Select the correct statement about how caching works in Active Model Serializers.
Attempts:
2 left
💡 Hint
Think about how cache keys and expiration are handled in serializers.
✗ Incorrect
Active Model Serializers caching requires you to manage cache keys manually. It does not automatically expire caches when object attributes change.