How do I include rails items in a JSON string object?

By : sacshu
Source: Stackoverflow.com
Question!

I'm trying to send this string to my Stripe account to process a transaction, and I want to send both an ID of the item being bought and the amount paid for as a JSON string. How do I include the pieces of the model in the string?

customer = Stripe::Customer.create(email: email, description: {"amount:" amount, "id:" idea_id}')

One part of the model is amount and the other is idea_id

By : sacshu


Answers

Assuming your model is called Product

@product = Product.find(params[:id])

customer = Stripe::Customer.create(email: email, description: {amount: @product.amount, id: @product.idea_id}.to_json)

or you can also use

customer = Stripe::Customer.create(email: email, description: @product.as_json(only: [:amount, :idea_id]))

However this may not work if you absolutely require the key for idea_id to be 'id'. in which case use the first option.

Hope this helps

By : SMathew


This video can help you solving your question :)
By: admin