1

Hello I'm trying to use as_json to output the parent object as an include. Here is my code :

photo.as_json(:include => [:comments, :likes])

This code works, this one doesn't :

photo.as_json(:include => [:comments, :likes, :user])

I get the error :

NoMethodError: undefined method `macro' for nil:NilClass

Any one ? Thanks :)

2
  • Seems "p" is Nil - how do you set photo and p ? Commented Feb 13, 2011 at 17:26
  • sorry, a typo from my part, it is "photo" for both. Commented Feb 14, 2011 at 9:42

3 Answers 3

4

Try

user = User.find(1)    
user.as_json(:include => {:photos => {:include => [:comments, :likes]}})
Sign up to request clarification or add additional context in comments.

1 Comment

I wanted the user as a sub object of my photo and not all photos.
1

I ended up using acts_as_api which allows for methods, templates and a lot of cool features that got the work done much easier.

1 Comment

You may like rabl or jbuilder, YMMV
0

you call the "methods" option instead:

photo.as_json(:methods => [:user], :include => [:comments, :likes, :user])

I've used this in Rails 4.0, ruby 2.0 to bring back what i need.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.