Questions tagged [active-model-serializers]

The purpose of `ActiveModel::Serializers` is to provide an object to encapsulate serialization of `ActiveModel` objects, including `ActiveRecord` objects.

Purpose

The purpose of ActiveModel::Serializers is to provide an object to encapsulate serialization of ActiveModel objects, including ActiveRecord objects.

Serializers know about both a model and the current_user, so you can customize serialization based upon whether a user is authorized to see the content.

In short, serializers replaces hash-driven development with object-oriented development.

Documentation

See the github project's README

739 questions
75
votes
7 answers

How to pass parameters to ActiveModel serializer

I'm using active model serializer. I have a model event which has_many activities. I want to return the event with the first n activities. I think I should pass the params n to the event serializer.
Bruce Lin
  • 2,700
  • 6
  • 28
  • 38
54
votes
6 answers

Conditional attributes in Active Model Serializers

How do I render an attribute only if some condition is true? For example, I want to render User's token attribute on create action.
Dan
  • 1,274
  • 2
  • 15
  • 32
48
votes
8 answers

How do you initialize an ActiveModel::Serializer class with an ActiveRecord::Relation array?

I have a serializer class FundingSerializer < ActiveModel::Serializer attributes :id, has_one :user has_one :tournament embed :ids, include: true end That initializes with the proper…
shicholas
  • 6,123
  • 3
  • 27
  • 38
43
votes
7 answers

Rails: Serializing deeply nested associations with active_model_serializers

I'm using Rails 4.2.1 and active_model_serializers 0.10.0.rc2 I'm new to API's and chose active_model_serializers because it seems to be becoming the standard for rails (Although I'm not opposed to using RABL or another serializer) The problem I'm…
Eric Norcross
  • 4,177
  • 4
  • 28
  • 53
39
votes
5 answers

Test ActiveModel::Serializer classes with Rspec

Given the following ActiveModel::Serializer class: class SampleSerializer < ActiveModel::Serializer attributes :id, :name end How can this be tested with RSpec?
gnerkus
  • 11,357
  • 6
  • 47
  • 71
38
votes
1 answer

jbuilder vs rails-api/active_model_serializers for JSON handling in Rails 4

I have started to begin with Rails 4. While working with handling of JSON format data, I found we can use rails/jbuilder and works well. However, When I was taking Codeschool's Rails 4 Pattern, they mentioned gem called active_model_serializers.…
36
votes
6 answers

Use active_model_serializer with a non-ActiveRecord object

I have a model object that is not descended from ActiveRecord::Base and is not stored in the database. I created a serializer for it (with the same name + "Serializer"), and in my controller I'm calling render json: object_instance. The result is an…
Robin Daugherty
  • 7,115
  • 4
  • 45
  • 59
28
votes
3 answers

Active Model Serializers belongs_to

This question pertains to AMS 0.8 I've got two models: class Subject < ActiveRecord::Base has_many :user_combinations has_ancestry end class UserCombination < ActiveRecord::Base belongs_to :stage belongs_to :subject belongs_to…
Jesper
  • 4,535
  • 2
  • 22
  • 34
23
votes
2 answers

Url Helpers in ActiveModelSerializer 0.10.0?

I know this version is still not officially released but I was checking out rc3 today and I noticed that I can no longer use Rails url helpers inside my serializers. In version 0.8.x, I could do the following: class BrandSerializer < BaseSerializer …
Jimmy Baker
  • 3,215
  • 1
  • 24
  • 26
23
votes
4 answers

Deserializing Json-Api with Rails Strong Parameters

I am using Active Model Serializers 0.10.x with EmberCLI and Rails while trying to have the Json-Api as the Adapter. GET requests are working, but deserialization for the Active Model is not even though I tried to implement the rails…
Deovandski
  • 790
  • 1
  • 8
  • 22
20
votes
1 answer

Rails Active Model Serializer - has_many and accessing the parent record

I'm trying to build a JSON representation of some Rails models using Active Model Serializer, where some models embed others. For example, I have Event and Attendees, Event has_and_belongs_to_many Attendees. class EventSerializer <…
Toms Mikoss
  • 9,097
  • 10
  • 29
  • 41
20
votes
3 answers

Rails active_model_serializer with pagination

I'm using active_model_serializer. Now I want to serialize an object with pagination, should I do the pagination logic in the controller or in the serializer? If I choose to do the pagination in serializer, I need to pass the page_number and…
19
votes
3 answers

Changing Active Model Serializers Default Adapter

If I want to switch from the default attributes adapter to the JSON API adapter, where would I do this? The getting started states this: Generally speaking, you as a user of AMS will write (or generate) these serializer classes. If you want to use a…
Jshoe523
  • 3,851
  • 2
  • 18
  • 21
17
votes
2 answers

Active Model Serializer - Increasing Render Performance

I'm running into a particular situation where the rendered json generated by ActiveModel::Serializer is extraordinary slow (around 6-8 seconds). How can I improve the speed of this rendering? Here's the code. Models: class Comment <…
1
2 3
49 50