Domanda

I am using active_model_serializers to create JSON for my Rails models.

serializer

class OptionSerializer < ActiveModel::Serializer
  self.root = false

  attributes :id

  def test_id
    object.id
  end
end

However, the to_json option seems to ignore the method added in OptionSerializer:

OptionSerializer.new(Option.find(13)).to_json.html_safe

expected output

{
  "id":      13,
  "test_id": 13
}

actual output

{
  "id": 13
}

I have reviewed this SO post, but that is the only post I can find where someone is experiencing this issue.

I am Running Ruby 1.9.3 and Rails 4.0.0. Thank you for your time.

Any support, input or recommendations would be very much appreciated.

È stato utile?

Soluzione

in the attributes list, you should specify test_id as well

attributes :id, :test_id
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top