Rails 3 respond_with json question -


having trouble generating json. trying render single active record result json this:

@data = user.find(1) respond_with(@data, :include => :status) 

the json result is:

{   -user: {     address: null     email: "test@email.com"     first_name: "test"     last_name: "man"     status_id: 1     username: "testguy"     status: { }   } } 

so whats problem? problem :include=>:status seems not bring on relation. in user model have belongs_to :status. how work on single result set?

when this:

@data = user.where("id = 1") respond_with(@data, :include => :status) 

the relation shows in json result set fine way. within array of objects, not want.

any ideas?

i assume status want include not 1 of http status codes (200, 201, etc.) nor object associated user object in way , own variable.

using rails 3.0.10 , ruby 1.9.2 may have find 1 of these 2 solutions suitable you. instead of respond_with use render follows.

solution 1

render :json => {:data => @data, :status => "whatever"} 

the json result is:

{   data:   {     user:     {       address: null       email: "test@email.com"       first_name: "test"       last_name: "man"       status_id: 1       username: "testguy"     }   }   status: "whatever" } 

solution 2

render :json => {:user => {:address => @data.address, :email => @data.email, ..., :status => "whatever"}} 

the json result is:

{   user:   {     address: null     email: "test@email.com"     ...     status: "whatever"   } } 

i hope looking for.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -