0

I have technical question i have user model which have many questions so in my API controller i do:

render json: user, :include => {
        :questions => {
            :only => [:text]
        },
    }

Now i want to add to JSON response question count. How can i do that in best way. I know that i can create method inside model : count_question and after that do:

render json: user, :include => {
        :questions => {
            :only => [:text]
        },
    }, :methods => [
        :count_question
    ]

and my response will be good. But is there better way to put that information to JSON. I want to avoid add method inside model. Is it possible to determine this count inside json renderer?

Greetings

1 Answer 1

1

Checkout JSON API and Active Model Serializers with Rails

This will hep you out format your JSON.

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

1 Comment

Thank You for advise. This is exacly what I was looking for. Upvoted

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.