Question

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.

Was it helpful?

Solution

in the attributes list, you should specify test_id as well

attributes :id, :test_id
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top