Conditional Active Model Serializer with Dates

Create a custom association to filter only future events based on a date and use that in the serializer.

In Feeds model, you will have something like below along with the actual association.

  has_many :events, if: -> { upcoming_event }, serializer: EventSerializer
  has_many :future_events, -> { where(date < Date.today) }, class_name: 'Event' # Change the name, conditions as per your requirement.

And access the custom association in the feed serializer,

class FeedSerializer < ActiveModel::Serializer
  
  has_many :future_events
 
end

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top