Active_model_serializers: Embedded association not using serializer

Created on 28 Mar 2018  路  7Comments  路  Source: rails-api/active_model_serializers

Expected behavior vs actual behavior

I have Instruments containing Series data. Each instrument has 64 series data points. I'd like to have series data embedded in the instrument, rather than side-loaded. When side-loaded, the series data is using its serializer. When embedded, it is not. I'm trying to get the embedded version to use a serializer. I'm using the json_api adapter.

Side-loaded version (is using SeriesSerializer):

 {
  "id": "4007",
  "type": "series",
  "attributes": {
    "series-date": "2013-01-31T00:00:00.000Z",
    "adjusted-close-price": "134.8589",
    "dividend-amount": "0.0",
    "created-at": "2018-03-20T18:32:19.514Z",
    "error": null
 },

Embedded version (is not using SeriesSerializer):

"id": "5",
  "type": "instruments",
  "attributes": {
    "symbol": "DIA",
    "name": "SPDR Dow Jones Industrial Average",
    "series": [
      {
        "id": 409119,
        "instrument_id": 5,
        "time_interval": "MA",
        "series_date": "2018-03-27T00:00:00.000Z",
        "open_price": "250.21",
        "high_price": "254.72",
        "low_price": "234.85",
        "close_price": "238.29",
        "adjusted_close_price": "238.29",
        "volume": "100172729.0",
        "dividend_amount": "0.454",
        "created_at": "2018-03-24T02:33:05.307Z",
        "updated_at": "2018-03-27T22:00:36.882Z"
      },

Steps to reproduce

Controller:
render json: instruments, each_serializer: InstrumentSeriesSerializer
Instrument Serializer:

class InstrumentSeriesSerializer < ActiveModel::Serializer
  attributes :id, :symbol, :name, :series
end

Series Serializer:

class SeriesSerializer < ActiveModel::Serializer
  attributes :id, :series_date, :adjusted_close_price, :dividend_amount, :created_at, :error
end

Environment

active_model_serializers (0.10.7)
ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-linux]
Ubuntu 16.04.4 LTS
Rails 5.1.5

Backtrace

Additonal helpful information

All 7 comments

What have you tried? Looks pretty straightforward to me

I figure I'm missing something. I've tried combinations of includes and has_manys, etc. I can't get the "series" data to use the serializer to limit the attributes. I'm assuming it should, but not really sure if an object's serializer is supposed to kick in when the object is embedded in a different class's object.

Same issue. Explicitly specifying the serializer on a deeply nested association is ignored, and the entire object is serialized.

Looks like the signature of has_many changed. It now expects serializer according to tests: https://github.com/rails-api/active_model_serializers/blob/v0.10.10/test/fixtures/poro.rb#L110

@jughead it doesn't require that and that's not the problem OP is having. OP wants to serialize some of the attributes as JSON objects using an AMS serializer

@jughead points for searching other issues, but no, that's talking about a different interface

Was this page helpful?
0 / 5 - 0 ratings

Related issues

NullVoxPopuli picture NullVoxPopuli  路  4Comments

axsuul picture axsuul  路  4Comments

adamcrown picture adamcrown  路  4Comments

PratheepV picture PratheepV  路  4Comments

iggant picture iggant  路  4Comments