0

I have a serializer with some has_many associations where I've modified the embed_key.

I'm trying to have the IDs for my model be like path-name rather than 12. The code below is making that happen:

class SymbolSerializer < ActiveModel::Serializer

  attributes  :id,
              :name,
              :parent_id,
              :rails_id

  embed :ids, include: true
  has_many  :ancestors, embed_key: :symbol_path, serializer: SimplifiedSymbolSerializer
  has_many  :children,  embed_key: :symbol_path, serializer: SimplifiedSymbolSerializer
  has_one   :parent,    embed_key: :symbol_path, serializer: SimplifiedSymbolSerializer
  has_many  :siblings,  embed_key: :symbol_path, serializer: SimplifiedSymbolSerializer

  def id
    object.symbol_path
  end

  def parent_id
    object.id
  end

  def rails_id
    object.id
  end
end

The problem is that I'd like to still have the actual id of the parent object (Which I've tried to do in my parent_id method ^^).

Right now the overridden method still returns the :symbol_path key rather than the id.

Anyone know how to address this?

BJ McDuck
  • 164
  • 1
  • 12

1 Answers1

0

The simplest way for me to address this was to just use a different naming convention for the key I wanted to pass.

So all I had to do was add this method/attribute in there too:

  def parent_rails_id
    object.parent.id
  end
BJ McDuck
  • 164
  • 1
  • 12