add to existing object in json array Rails 3 -
render :json mailbox.inbox.as_json(:include => [ {:messages => {:only => [:body]}}, {:recipients => {:only => [:firstname,:surname]}} ])
produces following output rails controller:
[ { "conversation": { "subject":"subject conversation 2", "messages" :[ {"body":"message 1"}, {"body":"message 2"} ], "recipients": [ {"firstname":"james","surname":"smith"}, {"firstname":"john","surname":"doe"} ] } } ]
what add current_user key:value pair each conversation object out sample this:
[ { "conversation": { "subject":"subject conversation 2", "messages" :[ {"body":"message 1"}, {"body":"message 2"}, ], "recipients": [ {"firstname":"james","surname":"smith"}, {"firstname":"john","surname":"doe"} ], "current_user" :[ {"id":10} ] } } ]
i don't have access conversation model in question (it's running off mailboxer gem) can't override as_json. appreciated
so, formalizing comments.
by looking apidock of json_as, can see way include arbitrary data add method retunrs data want use in model.
since not best thing in many ways , don't have access model, 1 way out customize json using popular gem called jbuilder, allows create more complex json.
Comments
Post a Comment