Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #6538

render to_json has_many array display

From elubin1234@gmail.com
Newsgroups comp.lang.ruby
Subject render to_json has_many array display
Date 2012-05-05 15:04 -0700
Organization http://groups.google.com
Message-ID <6574138.1980.1336255483591.JavaMail.geo-discussion-forums@ynff16> (permalink)

Show all headers | View raw


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?)

Back to comp.lang.ruby | Previous | Next | Find similar


Thread

render to_json has_many array display elubin1234@gmail.com - 2012-05-05 15:04 -0700

csiph-web