You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Thanks for verifying @wasifhossain. I knew this was an option but I wanted to avoid manually defining each association's trait manually if there was a better way.
I know the json structure is not standard. Unfortunately I'm making an api port from a legacy system so I can't change the contract at this time.
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 workExpected behavior vs actual behavior
I have a model
Account
with an associationbelongs_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:
But I want this:
Model + Serializer code
How can I achieve the expected behavior without writing each
account_status
field as an individual attribute in theAccountSerializer
??Controller
Model
Serializer
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:
The text was updated successfully, but these errors were encountered: