Skip to content

How to format associations to exclude root element? #2426

Open
@sarah-mica

Description

@sarah-mica

I'm using ActiveModel::Serializer to format my model data as json, but I would like to change the formatting so that the associations of my main model are not nested. I tried setting root: false and that doesn't work

Expected behavior vs actual behavior

I have a model Account with an association belongs_to :account_status
and I was able to add this association in the AccountSerializer to get that associated data just fine. But due to my api contract requirements, I need the json to be formatted without the association nesting.

So I'm getting this:

{
   "account_id": 1
   <other account info>
   ...
   "account_status": {
      "status_code": 1
      "desc": "status description"
      ....
   }
}

But I want this:

{
   "account_id": 1
   <other account info>
   ...
   "account_status_status_code": 1
   "account_status_desc": "status description"
   ....
}

Model + Serializer code

How can I achieve the expected behavior without writing each account_status field as an individual attribute in the AccountSerializer ??

Controller

class AccountsController < ActionController::API
  def show
    account = Account.find(params[:account_id])
    render json: account
  end
end

Model

class Account < ActiveRecord::Base
  self.primary_key = :account_id

  belongs_to :account_status, foreign_key: :account_status_code, inverse_of: :accounts

  validates :account_status_code, presence: true
end

Serializer

class AccountSerializer < ActiveModel::Serializer
  attributes(*Account.attribute_names.map(&:to_sym))

  belongs_to :account_status, 
             foreign_key: :account_status_code,
             inverse_of: :accounts
end

Environment

ActiveModelSerializers Version 0.10.0:

Output of ruby -e "puts RUBY_DESCRIPTION":
ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-darwin19]

OS Type & Version: macOS Catalina v 10.15.7

Integrated application and version Rails 6.1.4:

Metadata

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