Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paginate relations #323

Closed
Mziserman opened this issue Sep 24, 2018 · 1 comment
Closed

Paginate relations #323

Mziserman opened this issue Sep 24, 2018 · 1 comment

Comments

@Mziserman
Copy link

Mziserman commented Sep 24, 2018

Hello,

I'm using fast_jsonapi on a project and was wondering how to paginate relations without breaking the logic.

Currently, I have those serializers :

class EventTypeSerializer
  include FastJsonapi::ObjectSerializer
  attributes :id, :name

  has_many :events,
    if: Proc.new { |record| record.events.any? },
    serializer: EventSerializer
end
class EventSerializer
  include FastJsonapi::ObjectSerializer

  attributes :id, :name

  belongs_to :event_type,
    if: Proc.new { |record| record.event_type.present? },
    serializer: EventTypeSerializer
end

and this controller

class EventTypesController
  def show
    @event_type = EventType.find(params[:id])
    @options = {
      include: [:events], # can i do something at this level ?
      is_collection: false
    }
    render json: EventTypeSerializer.new(@event_type, @options).serialized_json
  end
end

The issue is some event type have thousands of events.
My question is, how can I paginate events on the event_type show view. Should I make a custom attribute for events in EventTypeSerializer ? Then events won't show in relationships. Should this logic be in the controller ?

@shishirmk
Copy link
Collaborator

One way i think of doing this is by sending links to page 1 instead of the ids https://github.com/Netflix/fast_jsonapi#links-on-a-relationship

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants