0

Have any way to return on has_many :roles a array instead of object?

class UserSerializer < ActiveModel::Serializer
  attributes :id, :name, :nickname, :image, :roles

  has_many :roles
end

Thanks.

2 Answers 2

1

You can build your own custom methods in the serializer and use those as attributes

class UserSerializer < ActiveModel::Serializer
  attributes :role_names

  def role_names
    object.roles.map(&:name)
  end
end
Sign up to request clarification or add additional context in comments.

Comments

0

You usually use includes for that. I'm not that familiar with using serializers though.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.