mr_muscle
mr_muscle

Reputation: 2900

Rails Fast Json API includes scope

In my Rails 6/Grape API app I've got a serializer where I want to include only active journeys (by active I mean journey.is_deleted: false). Current endpoint looks like:

helpers do
  def query
    current_user.journey_progresses.joins(:journey).where('is_deleted', false)
  end
end

get :journeys do
  ::Journeys::EnrolledJourneysSerializer.new(
    query,
    include: [:journey],
    class: { Journey: ::Journeys::JourneyListSerializer },
  )
end

It includes all journeys no matter if they have is_deleted: true or is_deleted: false. I want to include only journey with is_deleted: false to not show deleted journeys in the serialized response.

EnrolledJourneysSerializer

module Journeys
  class EnrolledJourneysSerializer
    include FastJsonapi::ObjectSerializer

    belongs_to :journey, serializer: JourneyListSerializer
    set_type :percent_progress
    attributes :percent_progress, :started_at
  end
end

JourneyListSerializer

module Journeys
  class JourneyListSerializer
    include FastJsonapi::ObjectSerializer

    attribute :content do |object|
      object.content.dig('attributes')
    end
  end
end

Is there any way different than default_scope on a Journey model?

Upvotes: 2

Views: 590

Answers (1)

Eyeslandic
Eyeslandic

Reputation: 14890

This line is wrong and needs to be changed to...

 current_user
  .journey_progresses
  .joins(:journey)
  .where(journeys: { is_deleted: false })

Upvotes: 1

Related Questions