fast_jsonapi limit includes returns, how to limit the included results of serialization

89 views Asked by At

I'm working on a rapid react js on rails prototype for a possible gig. I have finished the react UI portion and working on backend. The prototype monitors over 500 blogs and creates "lumps" which are just preview of articles. Sources are different types of blogs, and there are over 100 of those sources. When click on Sources, it pulls up all the "lumps" and displays them.

I'm having issue as returning all the "lumps" includes and just want all the new ones for the day. Currently, most sources have 500 to 1000 lumps, just want to limit the amount to the current day. I just have not found the proper code or query that will allow me to limit the results.

Using Rails6, fastjson_api and Ruby 3.02

**Source Controller**
module Api
    module V1
        class SourcesController < ApplicationController

        def show
            source = Source.find(params[:id])
            render json: SourceSerializer.new(source,options).serialized_json
         end

         def options            
            @options ||= { include: %i[lumps] }
         end


  end


          

**Source Serializer**
class SourceSerializer
  include FastJsonapi::ObjectSerializer
  attributes :title, :description, :image, :sourcetype, :category, :subcategory, :username, :first_name, :last_name, :email

 has_many :lumps
 has_many :urls
end


**Lump Serializer**

class LumpSerializer
  include FastJsonapi::ObjectSerializer
  attributes :body, :preview, :title, :link, :image, :description, :image
end

**Source Model**

class Source < ApplicationRecord
  has_many :lumps
  has_many :urls
  has_many :user_sources
  has_many :users, through: :user_sources
  validates :username, presence: true, uniqueness: {case_sensitive: false },
  length: {minimum: 3, maximum:25}
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
end

**Lump Model**
class Lump < ApplicationRecord
  belongs_to :source   
end

1

There are 1 answers

2
DaBus On

Simple answer, created a new verb in Source_Controller, added a route, and created date limited query. Passed the query to LumpSerializer and was done.