ruby - Rails, including relationships in json render -


i have following code returns json data includes @admin_user.companies:

@admin_user = user.find_by_email(email)  render :json=>{:status=>{:code=>200,:token=>@admin_user.authentication_token,     :user=> @admin_user,     :companies => @admin_user.companies }}   

each company have many "locations". how include locations every single company in @admin_user.companies in json?

the conventional way use

render json: @admin_user.companies, include: :locations 

(please refer #as_json more options.)

you don't need include status code in json, since it's in http headers. thus, following might close need.

render :json => @admin_user,   :include => {     :companies => { :include => :locations },   },   :methods => :authentication_token 

side note

this example. have configure :include , :methods want. more fine-grained control, jbuilder or rabl.


Comments

Popular posts from this blog

vb.net - Alternative to the T-SQL AS keyword -

php - MySQLi binding parameters in a prepared statement doesn't work unless inserted after "WHERE" -

ios - CFRelease causing crash in iPad application -