lrleon
lrleon

Reputation: 2648

rails 5 serializing an object along with other attributes

In a rails 5.2 app, I am having some problems for using a serializer when from a controller I perform some such this:

render json: { status: status, result: user, message: msg }

In this case, the render outputs all the fields of the User active record. However, for the object user I have defined a serializer in a way similar to this:

class UserSerializer < ActiveModel::Serializer
  attributes :id
end

And I have tested it successfully by executing an explicit renderization in this way

render json: user

Where user is, of course, an instance of User class. But, I have already mentioned, the serializer is not being invoked in the former situation (render json: { status: status, result: user, message: msg })

Some clue, idea, etc that helps me to guarantee that the serializer is invoked when the render is called through a hash containing the object?

Upvotes: 0

Views: 808

Answers (1)

mehedi
mehedi

Reputation: 466

Try this:

render json: { status: status, result: UserSerializer.new(user), message: msg }

Upvotes: 1

Related Questions