json - Simple join between two tables in Rails -


i building rails application work end apis. (using grape apis) have 2 tables (user, comment) user has many comments , comment belongs 1 users.

i trying return comments, , within comment object, want show user object user created comment.

i tried:

comment.includes(:user) 

and

comment.joins(:user).includes(:user) 

and none of them managed return sub-object. returns comment object (which has user_id) attribute).

is there way achieve in json format (as mentioned, use grape)

you can use to_json or as_json :include option. in api:

resources :comments   :id     comment.includes(:user).find(params[:id]).to_json(include: :user)   end end 

however, sooner or later need more control on json responses generated api. suggest take @ grape-rabl gem (or other solution building json - there plenty of them out there). grant finer control...


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -