Rails APIで複数のテーブルをincludesで結合して、その結果をNuxt.jsに渡して表示したいと思っております。
【Usersテーブル】has_many :posts
name
【Postsテーブル】belongs_to :user
title
user_id
上記のようなテーブルがある時、Railsでposts = Post.includes(:user)
のようにテーブルを結合すると、posts[0].user
のようにしたらユーザーのデータを取れますが、このposts
をNuxt.jsに渡してもuser
のデータをを取得することができません。
以下のようにしてJSONを渡しても、Nuxt.js側ではtitle
とuser_id
の値しか取得できないのですが、userテーブルのnameカラムのデータも合わせて渡すにはどのようにしたらいいのでしょうか?
rb
1# controllers/api/v1/posts_controller.rb 2 3module Api 4 module V1 5 class PostsController < ApplicationController 6 def index 7 posts = Post.includes(:user) 8 render json: { data: posts } 9 end 10 end 11end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/18 12:15