Skip to content

How to exclude an association in AMS 10 comparing to AMS 9 #1865

Open
@MartinKoerner

Description

Using rails 4, I have a domain object with a Serializer

class DomainObject < ActiveRecord::Base
  ...
end

class DomainObjectSerializer < ActiveModel::Serializer
  attributes :attr1, :attr2, :attr3
  has_many :foos
  has_many :bars
end

Using active_model_serializer v0.9.4 I could do

record = DomainObject.find(..)
serializer = DomainObjectSerializer.new(record, root: false, except: [:foos])
serializer.to_json

to get a JSON-String containing all the attributes and the association :bars, but not the association :foos.

After switching to active_model_serializer v0.10.2 this does not work anymore. The string contains all attributes. It seems, like the following gives me the same output:

record = DomainObject.find(..)
serializer = DomainObjectSerializer.new(record)
serializer.to_hash.to_json(except: [:foos])

But as the to_json call typically is done in the view layer, while the serializer is build inside a model or service, it seems not to be a good solution. Did I miss some configuration option?

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions