Rails で JSON を返すAPIを作成しています。
Postsテーブルに紐づくCommentsテーブルの取得件数にlimit
をかけたいのですが、どのように記述すればよいでしょうか??
例えば、1記事に対してそれぞれ3件ずつのコメントを出力する、ということを実現したいです。
post.rb :
Ruby
1class Post < ApplicationRecord 2 has_many : comments 3end
posts_controller.rb :
Ruby
1def index 2 @posts = Post.all.includes(:comments) 3 render json: @posts.to_json(:include => :comments) 4 end
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/22 09:26
2018/10/22 09:32
2018/10/22 10:42