active-model-serializers

Serialize an array of models using active_model_serializers

删除回忆录丶 提交于 2019-12-18 12:28:08
问题 I am trying to send the serialized version of a model to a view as a param, using the gem active_model_serializers #app/serializers/admin_serializer.rb class AdminSerializer < ActiveModel::Serializer attributes :id, :email, :access_locked? end #app/controllers/dashboard/admins_controller.rb def index @search = Admin.search(params[:q]) @admins = @search.result(:distinct => true).page(params[:page]).per(10) @page_entries_info = view_context.page_entries_info @admins # render json: @admins

Test ActiveModel::Serializer classes with Rspec

我只是一个虾纸丫 提交于 2019-12-18 11:44:14
问题 Given the following ActiveModel::Serializer class: class SampleSerializer < ActiveModel::Serializer attributes :id, :name end How can this be tested with RSpec ? 回答1: Assumptions This answer assumes you have the rspec-rails , active_model_serializers and factory_girl_rails gems installed and configured. This answer also assumes you have defined a factory for the Sample resource. Serializer spec For the current version(0.10.0.rc3) of active_model_serializers at the time of writing, ActiveModel

Eager load associations with Active Model Serializers

泪湿孤枕 提交于 2019-12-18 11:27:17
问题 Background I have a rails application with deeply nested associations. .-< WorkPeriod Timecard -< Week -< Day -<--< Subtotal `-< Adjustment -< (has many) I'm using Active Model Serializer to build out the API. On the client side I want to load a timecard and all it's associations in one shot. Currently my serializers look like this, class TimecardSerializer < ActiveModel::Serializer embed :ids, include: true has_many :weeks end class WeekSerializer < ActiveModel::Serializer embed :ids,

Limiting Associations Cascade in Active Model Serializer

给你一囗甜甜゛ 提交于 2019-12-18 10:31:22
问题 I'm having an issue with limiting the level of associations serialized within an active model resource. For example: A Game has many Teams which has many Players class GameSerializer < ActiveModel::Serializer attributes :id has_many :teams end class TeamSerializer < ActiveModel::Serializer attributes :id has_many :players end class PlayerSerializer < ActiveModel::Serializer attributes :id, :name end When I retrieve the JSON for the Team, it includes all the players in a sub array, as desired.

Versioning ActiveModel::Serializer

大憨熊 提交于 2019-12-13 19:53:40
问题 I'm using the gem active_model_serializers and I'm facing some issues with versioning. Controllers In app/controllers/v1/contracts_controller.rb module V1 class ContractsController < ApiController def index @contracts = Contract.all render json: @contracts end end end In app/controllers/v2/contracts_controller.rb module V2 class ContractsController < ApiController def index @contracts = Contract.all render json: @contracts end end end Serializers In app/serializers/v1/contract_serializer.rb

ActiveModel::Serializer in Rails - serializer methods ignored in JSON result

好久不见. 提交于 2019-12-13 14:13:44
问题 I am using active_model_serializers to create JSON for my Rails models. serializer class OptionSerializer < ActiveModel::Serializer self.root = false attributes :id def test_id object.id end end However, the to_json option seems to ignore the method added in OptionSerializer: OptionSerializer.new(Option.find(13)).to_json.html_safe expected output { "id": 13, "test_id": 13 } actual output { "id": 13 } I have reviewed this SO post, but that is the only post I can find where someone is

ActiveModel Serializers: has_many with condition at run-time?

こ雲淡風輕ζ 提交于 2019-12-13 13:04:17
问题 I use rails (5.0.1) and active_model_serializers (0.10.2). I would like to somehow conditionally serialize the has_many associations: class Question < ApplicationRecord has_many :responses, :inverse_of => :question end class Response < ApplicationRecord belongs_to :question, :inverse_of => :responses end class QuestionSerializer < ActiveModel::Serializer attributes :id, :title, :created_at, :updated_at has_many :responses end class ResponseSerializer < ActiveModel::Serializer attributes :id,

Force Active Model Serializer to return association

只谈情不闲聊 提交于 2019-12-13 03:45:41
问题 I have a Active Model Serializer that has the following: class API::DashboardSerializer < ActiveModel::Serializer attributes :id, :name, :special def special x = object.check_ins.first prev = x.prev_ci_with_weigh_in end end where special returns a record of class CheckIn and I'd like it to use the CheckInSerailizer for that record. How can I force it to use the CheckInSerializer in special ? 回答1: Remove special from attributes and then try has_one or belongs_to , described in this Guide, like

Rails ActiveModelSerializer, combine two lists of same-type-models into one serialized response, with different names

我怕爱的太早我们不能终老 提交于 2019-12-13 03:42:46
问题 I have a rails api, in which I'm using Active Model Serializers (version 0.10.6) to serializer json responses. I have two arrays, both are of type "FeatureRequest" in an endpoint that returns a list of requests a user has made, and a second list of requests that a user is tagged in. Ideally, I'd like to serialize the response to look something like this: { "my_requests" : { ...each serialized request... }, "tagged_requests" : { ...each serialized request, using the same serializer" } } Is

serialize date attributes

巧了我就是萌 提交于 2019-12-12 10:35:25
问题 I am using active_model_serializers and ember.js. One of my models has a date attribute. In rails date attributes are serialized in the format of "YYYY-MM-DD". The problem; when ember-data de-serializes the date using the javascript Date constructor it assumes an "incorrect" timezone. *Incorrect is not the best word but it is incorrect because I want it to default to the current timezone. DS.Model date attribute parses date (YYYY-MM-DD) incorrectly I am thinking the active_model_serializer