Reputation: 1
ActiveModelSerializers::SerializableResource.new(object.project, include: [collaborator: :pictures]).as_json[:project]
When object.project is nil then it occurs an issue. NoMethodError: undefined method `[]' for nil:NilClass
Upvotes: 0
Views: 646
Reputation: 724
It's not related to ActiveModelSerializers (AMS) after all. In Ruby, nil
causes a lot of NoMethodError
, so we often avoid nil
s with if
or unless
modifiers.
For example, in your case, we often do
ActiveModelSerializers::SerializableResource.new(object.project, include: [collaborator: :pictures]).as_json[:project] if object.project
Upvotes: 1