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
17
votes
2 answers

Eager load associations with Active Model Serializers

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…
17
votes
3 answers

Sideloading conditionally with ActiveModel::Serializers

I'm building an API using ActiveModel::Serializers. What is the best way to sideload data conditionally using params? So I can make requests like GET /api/customers: "customers": { "first_name": "Bill", "last_name": "Gates" } And GET…
Richard
  • 3,839
  • 5
  • 28
  • 34
16
votes
4 answers

Serialize an array of models using active_model_serializers

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,…
15
votes
1 answer

prettify JSON output of active-model-serializer in rails console

I am testing active-model-serializer output in the rails console and I am looking for a way to prettify the output. The the only solution I have found so far is: ap JSON.parse(ProfileSerializer.new(p).to_json) That seems like a roundabout approach.…
errata
  • 23,596
  • 2
  • 22
  • 32
13
votes
2 answers

ActiveModel serializer inheritance

say I have this serializer class FooSerializer < ActiveModel::Serializer attributes :this, :that, :the_other def this SomeThing.expensive(this) end def that SomeThing.expensive(that) end def…
patrick
  • 9,290
  • 13
  • 61
  • 112
13
votes
4 answers

Active Model Serializers: Adding extra information outside root in ArraySerializer

Say I have a model User and a serializer UserSerializer < ActiveModel::Serializer, and a controller that looks like this: class UsersController < ApplicationController respond_to :json def index respond_with User.all end end Now if I…
GMA
  • 5,816
  • 6
  • 51
  • 80
13
votes
2 answers

How to sort Rails Active Model serializers' response

Weirdly, I haven't find anything on the subject... How can I sort the JSON my Rails server delivers? I am currently using ActiveModel Serializer : embed :ids, include: true attributes :id, :name has_many :places I would like to sort the…
Justin D.
  • 4,946
  • 5
  • 36
  • 69
13
votes
2 answers

Use ActiveModel::Serializers to include two parent json arrays

I'm trying to send my front-end application json that looks like this: { facilities: [ {id: 5, name: 'happy days ranch', location: { address: '1424 Pastoral Lane', zipcode: '25245'}, instructor_ids: [2, 4, 9]} ], instructors: [ {id:…
13
votes
4 answers

Make root node in Active Model Serializer

I have an array of JSON in my Rails App in this format using Active Model Serializer: [ { "contact" : {} }, { "contact" : {} } ] How do I make it so that I remove one level of node above the contact USING active model serializer…
Madhan
  • 498
  • 1
  • 6
  • 19
13
votes
1 answer

Rails/Ember - active_model_serializer - undefined method `object' when sideloading

I'm trying to sideload data in active_model_serializer for an Ember application and get a NoMethodError when I attempt to include the objects: undefined method `object' for #Email:0x00000100d33d20 It only happens when :include => true is set, like…
user1938736
  • 443
  • 4
  • 9
12
votes
2 answers

Ember: Relationship link related data not loading / disappearing

I'm experiencing somewhat of a bug with Ember/Ember-data. Here's my scenario: Client lands on / route and Ember loads data from /api/v1/videos?limit=8. The response comes from a rails-api backend using active_model_serializers which ensures the…
Maros
  • 1,825
  • 4
  • 25
  • 56
12
votes
2 answers

Limiting Associations Cascade in Active Model Serializer

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…
Greg Olsen
  • 1,370
  • 17
  • 28
11
votes
2 answers

The correct way to version Rails 3 APIs

I have a Rails 3 engine which exposes API routes for around 20 controllers. Those controllers represent several different resources at various levels of nesting and are covered by over 500 rspec tests. The API is versioned at v1 using namespaces and…
Martin Foot
  • 3,594
  • 3
  • 28
  • 35
11
votes
3 answers

Using a different key name for an association attribute in rails api active model serializer

I am building a Rest API using rails-api and active-model-serializer to easily filter the required fields in the JSON. I am also using the has_one association in these serializers. All I wanted to know is how do I specify a different key name for…
swaroopsm
  • 1,389
  • 4
  • 18
  • 34
11
votes
7 answers

How to disable ActiveModel::Serializers for a specific controller?

We're using active_model_serializers - 0.8.1 in a Rails application. The app has some API specific controllers inheriting from ActionController::Metal in a way similar to rails-api's ActionController::API. Well we want to use…
Dimitris Zorbas
  • 5,187
  • 3
  • 27
  • 33
1
2
3
49 50