开发者

Rails as_json include parent object?

开发者 https://www.devze.com 2023-02-10 01:20 出处:网络
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, :like开发者_运维百科s])

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, :like开发者_运维百科s])

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 :)


Try

user = User.find(1)    
user.as_json(:include => {:photos => {:include => [:comments, :likes]}})


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消