{"action":"created","issue":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1539","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/1539/labels{/name}","comments_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1539/comments","events_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1539/events","html_url":"https://github.com/rails-api/active_model_serializers/issues/1539","id":136223757,"number":1539,"title":"JSON API loading unnecessary relationships","user":{"login":"bdmac","id":47326,"avatar_url":"https://avatars.githubusercontent.com/u/47326?v=3","gravatar_id":"","url":"https://api.github.com/users/bdmac","html_url":"https://github.com/bdmac","followers_url":"https://api.github.com/users/bdmac/followers","following_url":"https://api.github.com/users/bdmac/following{/other_user}","gists_url":"https://api.github.com/users/bdmac/gists{/gist_id}","starred_url":"https://api.github.com/users/bdmac/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bdmac/subscriptions","organizations_url":"https://api.github.com/users/bdmac/orgs","repos_url":"https://api.github.com/users/bdmac/repos","events_url":"https://api.github.com/users/bdmac/events{/privacy}","received_events_url":"https://api.github.com/users/bdmac/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/rails-api/active_model_serializers/labels/C:%20JSON%20API","name":"C: JSON API","color":"CCCCCC"},{"url":"https://api.github.com/repos/rails-api/active_model_serializers/labels/C:%20Relationships","name":"C: Relationships","color":"cccccc"},{"url":"https://api.github.com/repos/rails-api/active_model_serializers/labels/Performance","name":"Performance","color":"DDDDDD"},{"url":"https://api.github.com/repos/rails-api/active_model_serializers/labels/V:%200.10.x","name":"V: 0.10.x","color":"bfd4f2"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2016-02-24T23:33:56Z","updated_at":"2016-03-16T17:18:11Z","closed_at":"2016-03-15T17:09:53Z","body":"I've noticed that when I define associations in my serializers AMS winds up loading up a ton of data to meet the JSON API spec. Many of these relationships don't actually need to be loaded, specifically any belongs_to relationships.\r\n\r\nI get that we need to run a query to grab the resources in a has_many (or even a has_one) but if you have a belongs_to relationship the ID of the associated resource lives on the parent resource and unless it is also included, loading the associated resource is inefficient. Even in the case of has_many relationships it would seem like we could just load the IDs with pluck(:id) instead of loading up the entire model object unless include was specified.\r\n\r\nAre there any plans to optimize any of this? I currently find myself playing whack-a-mole with AMS queries. This is made especially hard if your API supports params[:include] since you don't really know what a client may request."},"comment":{"url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/comments/197439837","html_url":"https://github.com/rails-api/active_model_serializers/issues/1539#issuecomment-197439837","issue_url":"https://api.github.com/repos/rails-api/active_model_serializers/issues/1539","id":197439837,"user":{"login":"beauby","id":2215778,"avatar_url":"https://avatars.githubusercontent.com/u/2215778?v=3","gravatar_id":"","url":"https://api.github.com/users/beauby","html_url":"https://github.com/beauby","followers_url":"https://api.github.com/users/beauby/followers","following_url":"https://api.github.com/users/beauby/following{/other_user}","gists_url":"https://api.github.com/users/beauby/gists{/gist_id}","starred_url":"https://api.github.com/users/beauby/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/beauby/subscriptions","organizations_url":"https://api.github.com/users/beauby/orgs","repos_url":"https://api.github.com/users/beauby/repos","events_url":"https://api.github.com/users/beauby/events{/privacy}","received_events_url":"https://api.github.com/users/beauby/received_events","type":"User","site_admin":false},"created_at":"2016-03-16T17:18:11Z","updated_at":"2016-03-16T17:18:11Z","body":"Great summary of the situation @bdmac."}}