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

Remove 'attributes' nesting? #204

Open
davidmcginn opened this issue Apr 15, 2020 · 1 comment
Open

Remove 'attributes' nesting? #204

davidmcginn opened this issue Apr 15, 2020 · 1 comment

Comments

@davidmcginn
Copy link

Hi,
I have a class extending ActiveYaml::Base as below:

class Item < ActiveYaml::Base
  set_root_path Rails.root.join('db', 'static')
  field :name
end
# items.yml
- id: 1
  name: Animal Welfare
- id: 2
  name: Climate Change
- id: 3
  name: Asylum Seekers

However when I query the data, it always comes back nested under an attributes key.

irb(main):020:0> Item.all.as_json
=> [{"attributes"=>{"id"=>1, "name"=>"Animal Welfare"}}, {"attributes"=>{"id"=>2, "name"=>"Climate Change"}}, {"attributes"=>{"id"=>3, "name"=>"Asylum Seekers"}}]

irb(main):023:0> Item.first.as_json
=> {"attributes"=>{"id"=>1, "name"=>"Animal Welfare"}}

Is there a reason why this has to be the case, or can I remove it somehow? It's a bit annoying because I'll eventually want to start using ActiveRecord, and then we'll have to update the client to stop looking for that key or else start adding it in to the DB records.
Thanks

@davidmcginn
Copy link
Author

I just saw that there is an open PR for this: #196

In the meantime, adding this line to the class works:

  include ActiveModel::Serializers::JSON

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