I have a serializer which includes a belongs_to to another serializer. In my controller I want to specify the fields that the associated serializer returns. How do I do this? I have the following serializers:
class ProductSerializer < ActiveModel::Serializer
belongs_to :user
attributes :id, :name, :locked, :visible
end
class UserSerializer < ActiveModel::Serializer
attributes :id, :email, :name, :locked, :phone, :show_welcome
end
The following in my controller does not seem to work:
render json: @product, include: [:user], fields: [
:id, :name, :locked, :visible,
user: [:name, :email],
]
This does not work, however, and my user field returns back all of its fields.
My current workaround is to add a new attribute to ProductSerializer:
attribute :user do
{
name: object.user.name,
email: object.user.email,
}
end
But this feel like a hack to get around something I'm not fully grasping.
can you please make sure you are using the 0-10-stable HEAD
@typhoon2099 do you have any further updates on this? thanks
Not yet, I've been working on other things. I'll give this a go and report back soon.
I've just added 0-10-stable to my project and the situation seems to be the same as before. I can't seem to find much in the way of documentation around it but is it possible to limit the fields in the serializer? Eg:
class ProductSerializer < ActiveModel::Serializer
belongs_to :user, fields: [:name, :email]
attributes :id, :name, :locked, :visible
end
That kind of thing?
@typhoon2099 0-10-stable HEAD should support this feature. You can take a look at the test here: https://github.com/rails-api/active_model_serializers/blob/0-10-stable/test/adapter/json/fields_test.rb#L55
It should work for both :json and :attributes adapter in the same way, and for :json_api adapter in its own way.
Fields doc: https://github.com/rails-api/active_model_serializers/blob/0-10-stable/docs/general/fields.md
Let me know how it goes.
No, it doesn't seem to work, all the fields defined in UserSerializer are still being included. The tested code is the same as in my first comment.
closing the issue due to lack of further data related to debugging the case. Reiterating that the feature is now supported as mentioned in this issue.
the main comment: https://github.com/rails-api/active_model_serializers/issues/2330#issuecomment-490095465
feel free to open the issue again with necessary data that would help in debugging the case.
I encounted the bug in version 0.10.6 with adapter attributes.
@duyetpt can you please follow https://github.com/rails-api/active_model_serializers/issues/2330#issuecomment-489741918 and see if you still find the issue.
If yes, we would appreciate detailed info to help us debugging the case. Thanks!
When use version 0.10.6 or 0.10.9, i still encounter the bugs. I check code and see that, code in tag 0.10.6 and 0.10.9 is out date. After that, i try use gem with follow define:
gem 'active_model_serializers', git: 'https://github.com/rails-api/active_model_serializers', branch: '0-10-stable'
It worked for me.
yes because the issue was fixed recently after 0.10.9 was released.
yeah, thanks @wasifhossain.
you're welcome! @duyetpt