Need help generating a JSON object correctly

I currently have:

book = Book.find(book_id)
json = render :json => book.to_json(:only => [ :book_id, :description,
:isbn, :author, :title ])

However, I want to be able to add a field called book_id to book. I’m
pretty new to rails, and have no idea how to do this.

I’ve tried:

book = Book.find(book_id)
book.merge({ :book_id => params[:book] })
json = render :json => book.to_json(:only => [ :book_id, :description,
:isbn, :author, :title ])

But I get an error message saying that merge isn’t a method of book.
Any ideas?

Note: In case it isn’t obvious, book is the results of a database query
that finds a book_id.

I seem to have figured it out.

book = Book.find(book_id)
json = render :json => JSON::parse(book.to_json).merge(“book” => {
“passed_id” => params[:book] }).to_json

works, but I’m still curious if there’s a way to do it without messing
with the JSON.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs