简体   繁体   English

Rails 快速 Json API 包括 scope

[英]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).在我的 Rails 6/Grape API 应用程序中,我有一个序列化程序,我想只包含活动的旅程(活动的意思是 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 .它包括所有旅程,无论它们是否具有is_deleted: trueis_deleted: false I want to include only journey with is_deleted: false to not show deleted journeys in the serialized response.我只想包含带有is_deleted: false的旅程,以便在序列化响应中不显示已删除的旅程。

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? Journey model 上的 default_scope 有什么不同吗?

This line is wrong and needs to be changed to...此行错误,需要更改为...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM