1

I'm having some trouble trying to render multiple nested objects in json.

Here's my script

def getFullSale
        sale = Sale.find(params[:id])
        render json: sale, include: [:discount_sale, :offer_sale, :mixed_payment, :product_sale => {:include => {:product_history => {include: :product}}}, :refunds => {include: :refund_products}]
end

This works until :product_sale, however anything after is ignored.

What I need is to include both :product_sale and :refund with nested properties.

Thanks!

2 Answers 2

1

I've managed to make it work!

Credits in Here

The solution was to replace the array in the include and make it like an object.

def getFullSale
    sale = Sale.find(params[:id])
    render json: sale, include: {discount_sale: {}, offer_sale: {}, mixed_payment: {}, :product_sale => {:include => {:product_history => {include: :product}}}, :refunds => {include: :refund_products}}
end

I don't know why this happens, but now is working! And actually, changing the => to : is not a bad idea.

def getFullSale
    sale = Sale.find(params[:id])
    render json: sale, include: {discount_sale: {}, offer_sale: {}, mixed_payment: {}, product_sale: {include: {product_history: {include: :product}}}, refunds: {include: :refund_products}}
end
Sign up to request clarification or add additional context in comments.

Comments

0

Maybe it is a silly observation, but did you try changing your => to :

json: sale, include: [:discount_sale, :offer_sale, :mixed_payment, :product_sale : {:include : {:product_history : {include: :product}}}, :refunds : {include: :refund_products}]

1 Comment

Tried with render json: sale, include: [:discount_sale, :offer_sale, :mixed_payment, product_sale: {include: {product_history: {include: :product}}}, refunds: {include: :refund_products}] But it's the same response =(

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.