Let's say I have a Post that has many Comments. I have something like this in my model
class Post < AR:Base
...
PUBLIC_ATTRIBUTES = [:id, :title]
JSON_METHODS = [:comments]
...
def as_json(options={})
opts = {
only: PUBLIC_ATTRIBUTES,
methods: JSON_METHODS
}.merge(options){|k,o,n|o|n}
super(opts)
end
...
end
So in my controller I can just:
def show
post = Post.find ...
render json: { post: post }
end
JSON magically appears. The issue is when I have a lot of comments I want to paginate on. Also, the API is already in use, so people are using json[post][comments], otherwise i'd just add comments in like {post:post, comments:comments}
, but that's a no-go.