Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Build a Rails API Coding the API Returning JSON

Steven Fried
PLUS
Steven Fried
Courses Plus Student 5,638 Points

What is the correct answer to the code challenge for the as_json lesson (nesting)?

I have tried multiple times to get the answer to this code challenge, watched the video again and again, and I have no idea what I'm doing wrong. It would be helpful to have ready access to the correct answers so I can check myself without having to ask a question. If those are available, it is unclear where to find them.

monkeys_controller.rb
class MonkeysController < ApplicationController
  before_filter :find_monkey

  def show
    render json: @monkey.as_json(include::bananas)
  end

private
  def find_monkey
    @monkey = Monkey.find(params[:id])
  end
end

2 Answers

Tim Knight
Tim Knight
28,888 Points

Hi Steven,

You're really close. Includes is the attribute you're passing item(s) into. When passing in a symbol it's more common to use the older "hashrocket" syntax.

So instead of include::bananas which makes it look like you're trying to call a class on a module, you'd do something like this [attribute] => [symbol].

So putting that together:

render json: @monkey.as_json(:include => :bananas)