-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prevent undefined method for Array:Class error #2329
Conversation
This came about when adding fieldset support - #2223
Thank you @jpawlyn for your PR. would like to know your feedback on my comment above. |
Thanks @wasifhossain for the explanation and example. What I did for the array serialization test was to simplify a serializer from our app but I think I oversimplified it! I've added another top level attribute to better show the problem we're facing. If there's a solution that doesn't involve changing the code that would be really great. |
class Serializer | ||
class SerializationArrayTest < ActiveSupport::TestCase | ||
class ArraySerializer < ActiveModel::Serializer | ||
attributes :total, :items |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jpawlyn can you put a type
option here with any name you like and rerun all the tests again. I believe it should pass the tests.
type 'array'
attributes :total, :items # you can add any number of fields you like
The type
value is used at the top level of the json if you use the :json
adapter, but skipped for the :attributes
adapter.
You can find the details of this option here: https://github.com/rails-api/active_model_serializers/blob/0-10-stable/docs/general/serializers.md#type
Though it says this option is not useful with the :attributes
adapter, but providing it in the non-resource serializers like yours will help overcome the issue that you are having.
Let me know if you still find any issue after providing this option. Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's great, thanks a lot for your help with this @wasifhossain, much appreciated. I'll close the PR now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you are most welcome! glad that it worked for you 🙌
setup do | ||
@authors = [Author.new(id: 1, name: 'Blog Author')] | ||
@serializer_instance = ArraySerializer.new(@authors) | ||
@serializable = ActiveModelSerializers::SerializableResource.new(@authors, serializer: ArraySerializer, adapter: :attributes) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#2223 introduces the fields
option for the :attributes
adapter. So we can further trim down the fields that we want to render.
@serializable = ActiveModelSerializers::SerializableResource.new(
@authors,
serializer: ArraySerializer,
adapter: :attributes,
fields: [:items]
)
But now if we run the tests again with the code above, this change
if fieldset && fieldset.fields.any? |
NoMethodError: undefined method `model_name'
generated from
object.class.model_name.to_s.underscore |
No need for PR since using |
Purpose
Fix
NoMethodError: undefined method 'model_name' for Array:Class
. This occurs when serializing an Array object.This originates from adding fieldset support - #2223.
Changes
Add greater defensiveness when calling
json_key
.Caveats
Related GitHub issues
Additional helpful information