Im wondering if its possible to exclude an association given a specific controller action. Something like:
class BookSerializer < ApplicationSerializer
has_many :comments, if: -> { params[:action] == "index" } do
object.comments.ordered_by_creation
end
end
class BooksController < ApplicationController
def index
@books = Book.all
render json: @books, status: :ok
end
end
Is there any way to pass params[:action] to BookSerializer? Right now i'm managing this situation by extending BookSerializer and declaring the association there
class BookCommentsSerializer < BookSerializer
has_many :comments do
object.comments.ordered_by_creation
end
end
class BooksController < ApplicationController
def index
@books = Book.all
render json: @books, status: :ok
end
def show
render json: @book, serializer: BookCommentsSerializer, status: :ok
end
end