Skip to content

Embedded association not using serializer #2247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jcpny1 opened this issue Mar 28, 2018 · 7 comments
Closed

Embedded association not using serializer #2247

jcpny1 opened this issue Mar 28, 2018 · 7 comments

Comments

@jcpny1
Copy link

jcpny1 commented Mar 28, 2018

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

@bf4
Copy link
Member

bf4 commented Mar 29, 2018

What have you tried? Looks pretty straightforward to me

@jcpny1
Copy link
Author

jcpny1 commented Mar 29, 2018

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.

@oowowaee
Copy link

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

jughead added a commit to moteef/active_model_serializers that referenced this issue Dec 16, 2019
@jughead
Copy link

jughead commented Dec 16, 2019

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

@bf4
Copy link
Member

bf4 commented Dec 16, 2019

@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
Copy link

jughead commented Dec 17, 2019

Indeed, is that #2030 the right one?

@bf4
Copy link
Member

bf4 commented Dec 17, 2019

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

@bf4 bf4 closed this as completed Dec 17, 2019
@rails-api rails-api locked as off-topic and limited conversation to collaborators Dec 17, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants