1

How can I build a collection from two pre-serialized collections? In other words, I want to do something like this:

class ItemSerializer < ActiveModel::Serializer
  attributes :items

  has_many :things, :serializer => ThingSerializer
  has_many :other_things, :serializer => OtherThingSerializer

  def items
    things + other_things
  end

  def things
    object.things.where(:this => 'that')
  end

  def other_things
    object.other_things.where(:that => 'this')
  end
end

However, this does not work-- The individual serializers are not used.. The only way I can make this work is to do:

class ItemSerializer < ActiveModel::Serializer
  attributes :items

  def items
    things + other_things
  end

  def things
    object.things.where(:this => 'that').map do |thing|
      ThingSerializer.new(thing, options)
    end
  end

  def other_things
    object.other_things.where(:that => 'this').map do |other_thing|
      OtherThingSerializer.new(other_thing, options)
    end
  end
end

Which I would really like to avoid...

patrick
  • 9,290
  • 13
  • 61
  • 112
  • possible duplicate of [Use ActiveModel::Serializers to include two parent json arrays](http://stackoverflow.com/questions/16501972/use-activemodelserializers-to-include-two-parent-json-arrays) – AlexQueue Mar 16 '15 at 15:13
  • @AlexQueue, not at all related.. – patrick Mar 17 '15 at 18:10

0 Answers0