Tag: active model serializers

Rails Active Model Serializer – has_many并访问父记录

我正在尝试使用Active Model Serializer构建一些Rails模型的JSON表示,其中一些模型嵌入了其他模型。 例如,我有Event和Attendees,Event has_and_belongs_to_many Attendees。 class EventSerializer < ActiveModel::Serializer attributes :name has_many :attendees, serializer: AttendeeSerializer end class AttendeeSerializer < ActiveModel::Serializer attributes :name end 这将导致JSON像{ name: ‘Event One’, attendees: [{ name: ‘Alice’ }, { name: ‘Bob’ }] } 。 现在,我想补充与会者对此次活动所说的内容。 让我们说,评论belongs_to事件,belongs_to参加者。 我想在事件的序列化输出中包含所述评论,因此它将成为{ name: ‘Event One’, attendees: [{ name: ‘Alice’, comments: [{ text: ‘Event One was […]

活动模型序列化程序belongs_to

这个问题与AMS 0.8有关 我有两个型号: class Subject < ActiveRecord::Base has_many :user_combinations has_ancestry end class UserCombination < ActiveRecord::Base belongs_to :stage belongs_to :subject belongs_to :user end 还有两个序列化器: class UserCombinationSerializer < ActiveModel::Serializer attributes :id belongs_to :stage belongs_to :subject end class SubjectSerializer < ActiveModel::Serializer attributes :id, :name, :description, :subjects def include_subjects? object.is_root? end def subjects object.subtree end end 当UserCombination被序列化时,我想嵌入整个主题子树。 当我尝试使用此设置时,我收到此错误: undefined […]

如何为活动模型序列化器关系选择所需的属性

我使用JSONAPI格式和Active Model Serializers来创建带有rails-api的api 。 我有一个序列化程序,它显示了一个具有许多topics的特定post ,目前,在关系下,列出了这些主题。 它目前只列出id和类型。 我也想展示这个主题的标题。 有些人会说在我的控制器中使用include: ‘topics’ ,但我不需要完整的主题记录,只需要它的标题。 问题 :如何指定要从主题中显示哪些属性? 是)我有的 “data”: { “id”: “26”, “type”: “posts”, “attributes”: { “title”: “Test Title 11” }, “relationships”: { “topics”: { “data”: [ { “id”: “1”, “type”: “topics” } ] } } } 我想要的是 “data”: { “id”: “26”, “type”: “posts”, “attributes”: { “title”: “Test Title […]

使用active_model_serializers序列化模型数组

我试图使用gem active_model_serializers将模型的序列化版本作为参数发送到视图 #app/serializers/admin_serializer.rb class AdminSerializer true).page(params[:page]).per(10) @page_entries_info = view_context.page_entries_info @admins # render json: @admins respond_to do |format| format.html format.js format.json {render json: @admins} end end #app/views/dashboard/admins/index.html.erb 以上调试产生以下响应: — ‘{“data”:{“id”:”1″,”type”:”admins”,”attributes”:{“email”:”tech@bluesapling.com”,”access_locked?”:false}}}’ //returned by the first debug — – ‘{“object”:{“id”:36,”email”:”aubrey_schmitt@feeneykoch.io”,”created_at”:”2016-03-28T05:15:17.546Z”,”updated_at”:”2016-03-28T05:15:17.546Z”},”instance_options”:{},”root”:null,”scope”:null}’ – ‘{“object”:{“id”:20,”email”:”alysa_johnston@thompson.io”,”created_at”:”2016-03-28T05:15:16.304Z”,”updated_at”:”2016-03-28T05:15:16.304Z”},”instance_options”:{},”root”:null,”scope”:null}’ – ‘{“object”:{“id”:22,”email”:”kristofer.langosh@kunzeluettgen.com”,”created_at”:”2016-03-28T05:15:16.459Z”,”updated_at”:”2016-03-28T05:15:16.459Z”},”instance_options”:{},”root”:null,”scope”:null}’ – ‘{“object”:{“id”:37,”email”:”beryl_keler@wiza.biz”,”created_at”:”2016-03-28T05:15:17.624Z”,”updated_at”:”2016-03-28T05:15:17.624Z”},”instance_options”:{},”root”:null,”scope”:null}’ – ‘{“object”:{“id”:5,”email”:”wilhelmine_buckridge@crona.io”,”created_at”:”2016-03-28T05:15:15.139Z”,”updated_at”:”2016-03-28T05:15:15.139Z”},”instance_options”:{},”root”:null,”scope”:null}’ – ‘{“object”:{“id”:14,”email”:”edward_wisoky@corkery.net”,”created_at”:”2016-03-28T05:15:15.838Z”,”updated_at”:”2016-03-28T05:15:15.838Z”},”instance_options”:{},”root”:null,”scope”:null}’ – ‘{“object”:{“id”:27,”email”:”leonor@jerde.biz”,”created_at”:”2016-03-28T05:15:16.848Z”,”updated_at”:”2016-03-28T05:15:16.848Z”},”instance_options”:{},”root”:null,”scope”:null}’ – ‘{“object”:{“id”:2,”email”:”carley@wyman.net”,”created_at”:”2016-03-28T05:15:14.873Z”,”updated_at”:”2016-03-28T05:15:14.873Z”},”instance_options”:{},”root”:null,”scope”:null}’ – ‘{“object”:{“id”:10,”email”:”ervin.gleichner@cremin.org”,”created_at”:”2016-03-28T05:15:15.527Z”,”updated_at”:”2016-03-28T05:15:15.527Z”},”instance_options”:{},”root”:null,”scope”:null}’ – ‘{“object”:{“id”:15,”email”:”lonzo.dickens@johnscole.name”,”created_at”:”2016-03-28T05:15:15.916Z”,”updated_at”:”2016-03-28T05:15:15.916Z”},”instance_options”:{},”root”:null,”scope”:null}’ […]

使用active_model_serializers序列化权限(例如CanCan)

如何使用active_model_serializers序列化权限? 我没有访问current_user或can? 模型和序列化器中的方法。

使用active_model_serializers实现API版本控制的正确方法

我知道已经存在一些问题,而且这是一个关于AMS不能有效处理命名空间 (这种版本控制方法使用) 的开放性问题,但我想确保我在当前约束内处于正确的轨道。 现在我正在使用Rails 5和AMS 0.10.1,所以我做了以下事情: # config/initializers/active_model_serializer.rb ActiveModelSerializers.config.serializer_lookup_enabled = false 禁用默认的序列化程序查找(无论如何都不起作用); 和 # app/controllers/application_controller.rb class ApplicationController < ActionController::API def get_serializer(resource, options = {}) unless options[:each_serializer] || options[:serializer] then serializer = (self.class.name.gsub("Controller","").singularize + "Serializer").constantize resource.respond_to?(:to_ary) ? options[:each_serializer] = serializer : options[:serializer] = serializer end super(resource, options) end end 覆盖默认情况下如何找到序列化程序; 我的控制器和序列化器是这样的: # app/controllers/api/v2/api_controller.rb module Api::V2 class […]

使用Rspec测试ActiveModel :: Serializer类

给定以下ActiveModel::Serializer类: class SampleSerializer < ActiveModel::Serializer attributes :id, :name end 如何使用RSpec进行测试?

渴望与Active Model Serializers加载关联

背景 我有一个带有深层嵌套关联的rails应用程序。 .-< WorkPeriod Timecard -< Week -< Day -<–< Subtotal `-< Adjustment -< (has many) 我正在使用Active Model Serializer来构建API。 在客户端,我想一次性加载时间卡及其所有关联。 目前我的序列化器看起来像这样, class TimecardSerializer < ActiveModel::Serializer embed :ids, include: true has_many :weeks end class WeekSerializer < ActiveModel::Serializer embed :ids, include: true has_many :days end # … etc … 问题 这一切都找到了,除了没有任何东西急切加载。 因此,它最终会针对每个请求对数据库进行大量调用。 对于每周,它会单独请求该周的日期。 并且对于每一天,它单独请求它的work_periods,小计和调整。

在activemodel序列化程序中使用带有条件的属性

class ProjectSerializer < ActiveModel::Serializer attributes :id, :title end 我使用activemodel序列化程序返回一些条件的title属性。 通常我可以覆盖标题方法,但我想要的是确定是否返回title属性与条件。

ActiveModel :: Serializers Gem – Versioned API命名空间问题

我是Rails和模块/命名空间的新手 我的控制器是这样的命名空间: module Api module V1 class PostsController < ApiController ActiveModel :: Serializers在我的app文件夹中放了一个“Serializers”文件夹,在其中,我创建了包含以下代码的post_serializer.rb: class PostSerializer < ActiveModel::Serializer attributes :id, :body, :category, end 当我尝试访问JSON响应时,我得到: NameError at /api/v1/posts uninitialized constant Api::V1::PostsController::PostSerializer 这里的问题是什么?命名空间我的Serializers与API版本一起使用的最佳方法是什么?