I know how to disable the root element globally, aa Rails 3.1 include_root_in_json or using ActiveRecord::Base.include_root_in_json = false , but I want to do this only for a few JSON (not globally).
So far I have done it like this:
@donuts = Donut.where(:jelly => true) @coffees = Coffee.all @breakfast_sandwiches = Sandwich.where(:breakfast => true) dunkin_donuts_order = {} dunkin_donuts_order[:donuts] = @donuts dunkin_donuts_order[:libations] = @coffees dunkin_donuts_order[:non_donut_food] = @breakfast_sandwiches Donut.include_root_in_json = false Coffee.include_root_in_json = false render :json => dunkin_donuts_order Donut.include_root_in_json = true Coffee.include_root_in_json = true
There are about 5 cases where I have to do this, sometimes with more than one model, and it does not feel clean. I tried putting this in around_filter s, but exceptions upset the flow and it also got hairy.
There must be a better way!
source share