Skip to content
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

snake case not working in include #118

Open
matteo95g opened this issue Jun 19, 2020 · 0 comments
Open

snake case not working in include #118

matteo95g opened this issue Jun 19, 2020 · 0 comments

Comments

@matteo95g
Copy link

Hello,

I was trying to return a serializer with a relationship and I noticed that in the relationship field I was always getting { meta { included: false } }

At first, I thought it was not finding the correct serializer for the relationship... But after some time I realized that if I put the name of the model in camelCase in the included array it started working...

This is the index method in the controller:

def index
  render jsonapi: Club.all, include: [:cover, :fieldFolder]
end

These are the serializers:

class SerializableClub < SerializableBase
  attribute :name
  attribute :category
  attribute :area
  attribute :formal
  attribute :created_at
  attribute :updated_at

  has_one :cover
  has_one :field_folder
end
class SerializableFieldFolder < SerializableBase
  attribute :club_id

  belongs_to :club
end
class SerializableBase < JSONAPI::Serializable::Resource
  extend JSONAPI::Serializable::Resource::KeyFormat
  key_format ->(key) { key.to_s.camelize(:lower) }

  type { @object.class.name.underscore.pluralize }
end

The thing is that I'm transforming the keys to camelCase in the base serializer.

Is this the correct way to transform the keys? Or I should do it in a different way?

If keys transformation is ok, I don't think that it should affect the format of the include array, maybe calling underscore for each included key fix the issue.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant