Back to Guides

Overriding

#object

The object being serialized. The equivalent of self in the model class.

#scope

Allows you to include in the serializer access to an external method. See Authorization Scope for more.

#read_attribute_for_serialization(key)

The serialized value for a given key. e.g. read_attribute_for_serialization(:title) #=> 'Hello World'

Overriding

Associations

To override an association, call has_many, has_one or belongs_to with a block:

class PostSerializer < ActiveModel::Serializer
  has_many :comments do
    object.comments.active
  end
end

Attributes

To override an attribute, call attribute with a block:

class PostSerializer < ActiveModel::Serializer
  attribute :body do
    object.body.downcase
  end
end

results matching ""

    No results matching ""