Hint: Use commas and colons for options in render [OK]
Common Mistakes:
Using => instead of : in Ruby 3+ syntax
Missing commas between options
Using wrong keys like code instead of status
3. Given the controller code:
def show
@user = User.find(1)
render json: @user.to_json(only: [:id, :name])
end
What will the JSON response include?
medium
A. {"id":1,"name":"Alice"}
B. {"name":"Alice"}
C. {"id":1}
D. {"id":1,"name":"Alice","email":"alice@example.com"}
Solution
Step 1: Understand the use of to_json(only: [:id, :name])
This limits the JSON output to only the id and name fields of the user object.
Step 2: Identify which fields appear in the JSON response
Only id and name are included, so email is excluded.
Final Answer:
{"id":1,"name":"Alice"} -> Option A
Quick Check:
only: [:id, :name] = id and name fields only [OK]
Hint: only: limits JSON fields included [OK]
Common Mistakes:
Assuming all fields are included by default
Confusing only: with except:
Not calling to_json with options
4. What is wrong with this Rails controller code?
def index
@users = User.all
render json: @users only: [:id, :email]
end
medium
A. The @users variable should be a single user, not all users.
B. The only option should be inside to_json method, not passed directly to render.
C. You cannot render collections as JSON in Rails.
D. The render method requires a template name.
Solution
Step 1: Check how to limit JSON fields for collections
The only option must be passed inside to_json or as a render json: option hash, not as a separate argument.
Step 2: Identify the correct way to pass only option
Correct usage: render json: @users.to_json(only: [:id, :email]) or render json: @users, only: [:id, :email] inside a hash, but the given code passes only outside the hash.
Final Answer:
The only option should be inside to_json method, not passed directly to render. -> Option B
Quick Check:
only option must be inside to_json or render json hash [OK]
Hint: Pass only inside to_json or render json hash [OK]
Common Mistakes:
Passing only as separate argument outside render hash
Thinking collections can't be rendered as JSON
Forgetting to call to_json with options
5. You want to render a JSON response with a list of users, but only include users with active status true and only their id and name fields. Which code correctly does this in a Rails controller?
hard
A. render json: User.where(active: true) only: [:id, :name]
B. render json: User.all.select(:id, :name).where(active true)
C. render json: User.where(active: true).to_json(only: [:id, :name])
D. render json: User.where(active: true).only(:id, :name)
Solution
Step 1: Filter users by active status
Use User.where(active: true) to get only active users.
Step 2: Limit JSON fields to id and name
Call to_json(only: [:id, :name]) to include only those fields in the JSON output.
Step 3: Check each option for correctness
render json: User.where(active: true).to_json(only: [:id, :name]) correctly chains filtering and JSON field limiting. render json: User.all.select(:id, :name).where(active: true) has wrong method order and will error. render json: User.where(active: true), only: [:id, :name] passes only incorrectly. render json: User.where(active: true).only(:id, :name) uses a non-existent only method on ActiveRecord relation.
Final Answer:
render json: User.where(active: true).to_json(only: [:id, :name]) -> Option C
Quick Check:
Filter then to_json with only: fields [OK]
Hint: Filter first, then call to_json with only: [OK]
Common Mistakes:
Using only as a method on ActiveRecord relation
Passing only outside to_json or render json hash
Selecting fields before filtering causing errors
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.