0

Consider a controller action that returns the following:

Post.includes(:comments).to_json(:include => [:comments])

Is it somehow possible to pass arguments to the to_json comments method call so that I could modify comments json representation in this place rather than doing it in Comment model?

1 Answer 1

1

to_json only accepts a list of options, per the documentation... If you absolutely had to, you could monkey-patch to_json to do whatever work you needed to do, then call super to execute the default to_json, but I wouldn't recommend this.

In your situation, based on what I understand you're describing, it's probably best to simply do it either on the Comment model, or as a private method on the controller.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for that reference. I just noticed that the last example (2nd level and higher order associations) is exactly what I was looking for!

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.