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

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -