#7 ぼちぼち

This commit is contained in:
2025-05-23 01:02:12 +09:00
parent 59678cf8b9
commit db430cc426
7 changed files with 158 additions and 8 deletions
+17 -3
View File
@@ -3,14 +3,28 @@ class PostsController < ApplicationController
# GET /posts
def index
@posts = Post.all
if params[:tags].present?
tag_names = params[:tags].split(',')
match_type = params[:match]
if match_type == 'any'
@posts = Post.joins(:tags).where(tags: { name: tag_names }).distinct
else
@posts = Post.joins(:tags)
tag_names.each do |tag|
@posts = @posts.where(id: Post.joins(:tags).where(tags: { name: tag }))
end
@posts = @posts.distinct
end
else
@posts = Post.all
end
render json: @posts
end
# GET /posts/1
def show
render json: @post
@post = Post.includes(:tags).find(params[:id])
render json: @post.as_json(include: { tags: { only: [:id, :name] } })
end
# POST /posts