{"action":"created","issue":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1284","repository_url":"https://api.github.com/repos/rails-api/active_model_serializers","labels_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1284/labels{/name}","comments_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1284/comments","events_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1284/events","html_url":"https://github.com/rails-api/active_model_serializers/pull/1284","id":112431018,"number":1284,"title":"Add optional fields support for associations","user":{"login":"vasilakisfil","id":590575,"avatar_url":"https://avatars.githubusercontent.com/u/590575?v=3","gravatar_id":"","url":"https://api.github.com/users/vasilakisfil","html_url":"https://github.com/vasilakisfil","followers_url":"https://api.github.com/users/vasilakisfil/followers","following_url":"https://api.github.com/users/vasilakisfil/following{/other_user}","gists_url":"https://api.github.com/users/vasilakisfil/gists{/gist_id}","starred_url":"https://api.github.com/users/vasilakisfil/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vasilakisfil/subscriptions","organizations_url":"https://api.github.com/users/vasilakisfil/orgs","repos_url":"https://api.github.com/users/vasilakisfil/repos","events_url":"https://api.github.com/users/vasilakisfil/events{/privacy}","received_events_url":"https://api.github.com/users/vasilakisfil/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/rails-api/active_model_serializers/labels/Feature","name":"Feature","color":"009800"},{"url":"https://api.github.com/repos/rails-api/active_model_serializers/labels/Needs%20Submitter%20Response","name":"Needs Submitter Response","color":"eb6420"},{"url":"https://api.github.com/repos/rails-api/active_model_serializers/labels/V:%200.10.x","name":"V: 0.10.x","color":"bfd4f2"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":17,"created_at":"2015-10-20T18:32:01Z","updated_at":"2016-03-16T13:57:02Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1284","html_url":"https://github.com/rails-api/active_model_serializers/pull/1284","diff_url":"https://github.com/rails-api/active_model_serializers/pull/1284.diff","patch_url":"https://github.com/rails-api/active_model_serializers/pull/1284.patch"},"body":"I am really fond of JSONAPI but there are people that they still need JSON (mostly) and Attributes adapters :) Plus, we should opt for best migration from 0.8.x and 0.9x adapters.\r\n\r\nThere is a minor bug in the fields option in master branch. If you have:\r\n\r\n```ruby\r\nrender json: @videos.limit(1), each_serializer: VideoSerializer,  fields: [:id],\r\n```\r\n\r\nThis will apply the fields to all associations too. We don't want that. What we want is:\r\n\r\nIf there is a fields param, apply it to the main resource. If there are associations, serialize the associations without removing any attributes. For instance:\r\n```ruby\r\nrender json: @videos.limit(1), each_serializer: VideoSerializer,  fields: [:id]\r\n```\r\nThen this should apply the `fields: [:id]` only in the VideoSerializer and not in any association.\r\n\r\nIf there are associations in the fields array (just like in StrongParams) and these also exist in the main resource, apply the associations fields there. For instance:\r\n```ruby\r\nrender json: @videos.limit(1), each_serializer: VideoSerializer,  fields: [:id, user: [:name]]\r\n```\r\nThen this should apply the `fields: [:id]` only in the VideoSerializer and if there is a user association, apply `fields: [:name]` in the UserSerializer.\r\n\r\nI am really looking forward to merge this PR so if you need anything from me just ping me. I will also create a regression test highlighting the bug."},"comment":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/comments/197342902","html_url":"https://github.com/rails-api/active_model_serializers/pull/1284#issuecomment-197342902","issue_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1284","id":197342902,"user":{"login":"vasilakisfil","id":590575,"avatar_url":"https://avatars.githubusercontent.com/u/590575?v=3","gravatar_id":"","url":"https://api.github.com/users/vasilakisfil","html_url":"https://github.com/vasilakisfil","followers_url":"https://api.github.com/users/vasilakisfil/followers","following_url":"https://api.github.com/users/vasilakisfil/following{/other_user}","gists_url":"https://api.github.com/users/vasilakisfil/gists{/gist_id}","starred_url":"https://api.github.com/users/vasilakisfil/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vasilakisfil/subscriptions","organizations_url":"https://api.github.com/users/vasilakisfil/orgs","repos_url":"https://api.github.com/users/vasilakisfil/repos","events_url":"https://api.github.com/users/vasilakisfil/events{/privacy}","received_events_url":"https://api.github.com/users/vasilakisfil/received_events","type":"User","site_admin":false},"created_at":"2016-03-16T13:57:01Z","updated_at":"2016-03-16T13:57:01Z","body":"The last monkeypatch is irrelevant actually, it allows you to take out an item from a collection just before serializing."}}