fast_jsonapi limit包含返回,如何限制包含的序列化结果



我正在为可能的演出开发一个快速反应的js-on-rails原型。我已经完成了react UI部分并在后台工作。该原型监视了500多个博客;块状物";这些只是文章的预览。来源是不同类型的博客,其中有100多个来源。当点击Sources时,它会调出所有";块状物";并显示它们。

我遇到了问题,因为我把所有的";块状物";包括并且只想要当天的所有新的。目前,大多数来源都有500到1000块,只是想将数量限制在当天。我只是没有找到合适的代码或查询来限制结果。

使用Rails6、fastjson_api和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-zd-.]+.[a-z]+z/i
end
**Lump Model**
class Lump < ApplicationRecord
belongs_to :source   
end

简单答案,在Source_Controller中创建了一个新动词,添加了一个路由,并创建了日期限制查询。将查询传递给LumpSerializer并完成。

最新更新