def list
@rings = Ring.order("RAND()")
#JSON RENDERING
render :json => @rings.to_json(:include => [:variations, :stones]), :callback => params[:callback]
end
def show
@showring = Ring.includes(:stones, :variations).find(params[:id])
@other_rings = Ring.select([:id, :stone_count]).where(:style_number => @showring.style_number).reject{ |ring| ring == @showring}
#JSON RENDERING
render :json => {@showring.to_json(:include =>[:variations, :stones]), :other_rings => @other_rings}, :callback => params[:callback]
end
My list view rendering works fine, but when i want to do a show view, with two objects, and showring with includes won’t render proper JSON. It is quoting everything in the object with the includes…
JSON output looks like this:
showring => “{“available”:”yes”,”eng…9″,”stone_y”:”149.4″}]}”
other_rings => properly rendered object
On a seperate note, if i have already added the includes to @rings object, why do i then again have to add the association in the “to_json” method?
When you do
Rails is converting @showring to json (ie getting back a string representation), i.e. the value is the string literal. Instead do
as_jsondoes all the work of turning the object into a hash but without the final step of turning into a string