Skip to content

Getting the serialized hash without render #1101

Closed
@PericlesTheo

Description

Hey humans,

Let's say I have

class MessageSerializer < ActiveModel::Serializer
  attributes :content

  belongs_to :user
end

When I use render json: @message in the controller I get {:message=>{:content=>"some peoisadfnsaidfnple like wefaefsushi", :user=>{:email=>"hi@example.com"}}} which is what I'd expect.

However, I'd like to be able to get the same serialization without using render in the controller.

I couldn't find anything in the docs and the only way around I found is

ActiveModel::Serializer::Adapter::Json.new(MessageSerializer.new(Message.last)).serializable_hash

Is there an easier way of achieving the above?

Cheers!

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions