Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

render to_json has_many array display

14 views
Skip to first unread message

elubi...@gmail.com

unread,
May 5, 2012, 6:04:43 PM5/5/12
to
I have an ActiveRecord class with a has_many of another class. All works fine to query the first and add the second with to_json. However, how can i display the class B data in a more precise format?

ss = ShirtSet.select("shirt_sets.id")
render :json => ss.to_json(:include => {:shirt => {:only => :name}})

[{"id":"SHIRT SET 1","shirts":[{"name":"shirt1"},{"name":"shirt 2"},{"name":"shirt 3"}]}]

when what i really want is:

[{"id":"SHIRT SET 1","shirts":["shirt1","shirt 2","shirt 3"}]

is there a way to easily do that with to_json (maybe the :method helper?)
0 new messages