{"action":"created","comment":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/comments/42550831","id":42550831,"diff_hunk":"@@ -35,8 +35,19 @@ def serializable_hash_for_single_resource(options)\n \n         def resource_relationships(options)\n           relationships = {}\n+\n+          association_fields_hash = {}\n+          if options[:fields]\n+            association_fields_hash = Hash[\n+              *options[:fields].select { |s| s.is_a? Hash }\n+            ]\n+          end\n+\n           serializer.associations(@include_tree).each do |association|\n-            relationships[association.key] = relationship_value_for(association, options)\n+            relationships[association.key] = relationship_value_for(\n+              association,\n+              options.merge(fields: association_fields_hash[association.name])","path":"lib/active_model/serializer/adapter/attributes.rb","position":16,"original_position":16,"commit_id":"492b9c9c6928459ee557b7773f809d098581d613","original_commit_id":"492b9c9c6928459ee557b7773f809d098581d613","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},"body":"not the case since this runs only for associations. The case you said would only happen with the following (given that there is a user association):\r\n```\r\nrender json: @videos.limit(1), each_serializer: VideoSerializer,  fields: [:id, user: []]\r\n```\r\n\r\nPlus, test would cry out if that was true.\r\n\r\n","created_at":"2015-10-20T20:40:46Z","updated_at":"2015-10-20T20:40:46Z","html_url":"https://github.com/rails-api/active_model_serializers/pull/1284#discussion_r42550831","pull_request_url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1284","_links":{"self":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/comments/42550831"},"html":{"href":"https://github.com/rails-api/active_model_serializers/pull/1284#discussion_r42550831"},"pull_request":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1284"}}},"pull_request":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1284","id":48239533,"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","issue_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1284","number":1284,"state":"open","locked":false,"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},"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.","created_at":"2015-10-20T18:32:01Z","updated_at":"2015-10-20T20:40:46Z","closed_at":null,"merged_at":null,"merge_commit_sha":"f2de343d9487d5db2799c35f8175177b8645179f","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1284/commits","review_comments_url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1284/comments","review_comment_url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/comments{/number}","comments_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1284/comments","statuses_url":"https://api.github.com/repos/rails-api/active_model_serializers/statuses/492b9c9c6928459ee557b7773f809d098581d613","head":{"label":"vasilakisfil:feat/nested_fields","ref":"feat/nested_fields","sha":"492b9c9c6928459ee557b7773f809d098581d613","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},"repo":{"id":44604370,"name":"active_model_serializers","full_name":"vasilakisfil/active_model_serializers","owner":{"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},"private":false,"html_url":"https://github.com/vasilakisfil/active_model_serializers","description":"ActiveModel::Serializer implementation and Rails hooks","fork":true,"url":"https://api.github.com/repos/vasilakisfil/active_model_serializers","forks_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/forks","keys_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/keys{/key_id}","collaborators_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/teams","hooks_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/hooks","issue_events_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/issues/events{/number}","events_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/events","assignees_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/assignees{/user}","branches_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/branches{/branch}","tags_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/tags","blobs_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/git/refs{/sha}","trees_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/git/trees{/sha}","statuses_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/statuses/{sha}","languages_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/languages","stargazers_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/stargazers","contributors_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/contributors","subscribers_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/subscribers","subscription_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/subscription","commits_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/commits{/sha}","git_commits_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/git/commits{/sha}","comments_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/comments{/number}","issue_comment_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/issues/comments{/number}","contents_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/contents/{+path}","compare_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/compare/{base}...{head}","merges_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/merges","archive_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/downloads","issues_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/issues{/number}","pulls_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/pulls{/number}","milestones_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/milestones{/number}","notifications_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/labels{/name}","releases_url":"https://api.github.com/repos/vasilakisfil/active_model_serializers/releases{/id}","created_at":"2015-10-20T12:21:36Z","updated_at":"2015-10-20T12:21:37Z","pushed_at":"2015-10-20T18:53:19Z","git_url":"git://github.com/vasilakisfil/active_model_serializers.git","ssh_url":"git@github.com:vasilakisfil/active_model_serializers.git","clone_url":"https://github.com/vasilakisfil/active_model_serializers.git","svn_url":"https://github.com/vasilakisfil/active_model_serializers","homepage":"","size":7838,"stargazers_count":0,"watchers_count":0,"language":"Ruby","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"rails-api:master","ref":"master","sha":"737784c9b702ea47932e8e6a1812d445f115bf7d","user":{"login":"rails-api","id":2820360,"avatar_url":"https://avatars.githubusercontent.com/u/2820360?v=3","gravatar_id":"","url":"https://api.github.com/users/rails-api","html_url":"https://github.com/rails-api","followers_url":"https://api.github.com/users/rails-api/followers","following_url":"https://api.github.com/users/rails-api/following{/other_user}","gists_url":"https://api.github.com/users/rails-api/gists{/gist_id}","starred_url":"https://api.github.com/users/rails-api/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rails-api/subscriptions","organizations_url":"https://api.github.com/users/rails-api/orgs","repos_url":"https://api.github.com/users/rails-api/repos","events_url":"https://api.github.com/users/rails-api/events{/privacy}","received_events_url":"https://api.github.com/users/rails-api/received_events","type":"Organization","site_admin":false},"repo":{"id":2889230,"name":"active_model_serializers","full_name":"rails-api/active_model_serializers","owner":{"login":"rails-api","id":2820360,"avatar_url":"https://avatars.githubusercontent.com/u/2820360?v=3","gravatar_id":"","url":"https://api.github.com/users/rails-api","html_url":"https://github.com/rails-api","followers_url":"https://api.github.com/users/rails-api/followers","following_url":"https://api.github.com/users/rails-api/following{/other_user}","gists_url":"https://api.github.com/users/rails-api/gists{/gist_id}","starred_url":"https://api.github.com/users/rails-api/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rails-api/subscriptions","organizations_url":"https://api.github.com/users/rails-api/orgs","repos_url":"https://api.github.com/users/rails-api/repos","events_url":"https://api.github.com/users/rails-api/events{/privacy}","received_events_url":"https://api.github.com/users/rails-api/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/rails-api/active_model_serializers","description":"ActiveModel::Serializer implementation and Rails hooks","fork":false,"url":"https://api.github.com/repos/rails-api/active_model_serializers","forks_url":"https://api.github.com/repos/rails-api/active_model_serializers/forks","keys_url":"https://api.github.com/repos/rails-api/active_model_serializers/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rails-api/active_model_serializers/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rails-api/active_model_serializers/teams","hooks_url":"https://api.github.com/repos/rails-api/active_model_serializers/hooks","issue_events_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/events{/number}","events_url":"https://api.github.com/repos/rails-api/active_model_serializers/events","assignees_url":"https://api.github.com/repos/rails-api/active_model_serializers/assignees{/user}","branches_url":"https://api.github.com/repos/rails-api/active_model_serializers/branches{/branch}","tags_url":"https://api.github.com/repos/rails-api/active_model_serializers/tags","blobs_url":"https://api.github.com/repos/rails-api/active_model_serializers/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rails-api/active_model_serializers/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rails-api/active_model_serializers/git/refs{/sha}","trees_url":"https://api.github.com/repos/rails-api/active_model_serializers/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rails-api/active_model_serializers/statuses/{sha}","languages_url":"https://api.github.com/repos/rails-api/active_model_serializers/languages","stargazers_url":"https://api.github.com/repos/rails-api/active_model_serializers/stargazers","contributors_url":"https://api.github.com/repos/rails-api/active_model_serializers/contributors","subscribers_url":"https://api.github.com/repos/rails-api/active_model_serializers/subscribers","subscription_url":"https://api.github.com/repos/rails-api/active_model_serializers/subscription","commits_url":"https://api.github.com/repos/rails-api/active_model_serializers/commits{/sha}","git_commits_url":"https://api.github.com/repos/rails-api/active_model_serializers/git/commits{/sha}","comments_url":"https://api.github.com/repos/rails-api/active_model_serializers/comments{/number}","issue_comment_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/comments{/number}","contents_url":"https://api.github.com/repos/rails-api/active_model_serializers/contents/{+path}","compare_url":"https://api.github.com/repos/rails-api/active_model_serializers/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rails-api/active_model_serializers/merges","archive_url":"https://api.github.com/repos/rails-api/active_model_serializers/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rails-api/active_model_serializers/downloads","issues_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues{/number}","pulls_url":"https://api.github.com/repos/rails-api/active_model_serializers/pulls{/number}","milestones_url":"https://api.github.com/repos/rails-api/active_model_serializers/milestones{/number}","notifications_url":"https://api.github.com/repos/rails-api/active_model_serializers/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rails-api/active_model_serializers/labels{/name}","releases_url":"https://api.github.com/repos/rails-api/active_model_serializers/releases{/id}","created_at":"2011-12-01T06:39:26Z","updated_at":"2015-10-20T20:22:58Z","pushed_at":"2015-10-20T18:56:47Z","git_url":"git://github.com/rails-api/active_model_serializers.git","ssh_url":"git@github.com:rails-api/active_model_serializers.git","clone_url":"https://github.com/rails-api/active_model_serializers.git","svn_url":"https://github.com/rails-api/active_model_serializers","homepage":"","size":7838,"stargazers_count":3066,"watchers_count":3066,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":706,"mirror_url":null,"open_issues_count":172,"forks":706,"open_issues":172,"watchers":3066,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1284"},"html":{"href":"https://github.com/rails-api/active_model_serializers/pull/1284"},"issue":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1284"},"comments":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1284/comments"},"review_comments":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1284/comments"},"review_comment":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/pulls/1284/commits"},"statuses":{"href":"https://api.github.com/repos/rails-api/active_model_serializers/statuses/492b9c9c6928459ee557b7773f809d098581d613"}}}}