首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >fast_jsonapi限制包括返回,如何限制所包含的序列化结果

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

Stack Overflow用户
提问于 2022-03-27 23:20:18
回答 1查看 41关注 0票数 0

我正在为一个可能的工作做一个快速反应js的rails原型。我已经完成了react部分和后端的工作。原型监控超过500个博客,并创建“转储”,这只是文章的预览。源是不同类型的博客,有100多个这样的来源。当点击源时,它会拉出所有的“转储”并显示它们。

我有问题,因为我还所有的“块”包括,只是希望所有的新的一天。目前,大多数来源有500至1000块,只是想限制金额到目前的一天。我只是没有找到合适的代码或查询来限制结果。

使用Rails6、fastjson_api和Ruby3.02

代码语言:javascript
复制
**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
EN

回答 1

Stack Overflow用户

发布于 2022-03-27 23:39:52

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71641020

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档