{"action":"created","issue":{"url":"https://api.github.com/repos/rails/rails/issues/18312","labels_url":"https://api.github.com/repos/rails/rails/issues/18312/labels{/name}","comments_url":"https://api.github.com/repos/rails/rails/issues/18312/comments","events_url":"https://api.github.com/repos/rails/rails/issues/18312/events","html_url":"https://github.com/rails/rails/issues/18312","id":53307579,"number":18312,"title":"Eager-loaded nil associations result in N+1 query problem on Rails 4.1 & 4.2 when using `references`","user":{"login":"codeodor","id":23479,"avatar_url":"https://avatars.githubusercontent.com/u/23479?v=3","gravatar_id":"","url":"https://api.github.com/users/codeodor","html_url":"https://github.com/codeodor","followers_url":"https://api.github.com/users/codeodor/followers","following_url":"https://api.github.com/users/codeodor/following{/other_user}","gists_url":"https://api.github.com/users/codeodor/gists{/gist_id}","starred_url":"https://api.github.com/users/codeodor/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/codeodor/subscriptions","organizations_url":"https://api.github.com/users/codeodor/orgs","repos_url":"https://api.github.com/users/codeodor/repos","events_url":"https://api.github.com/users/codeodor/events{/privacy}","received_events_url":"https://api.github.com/users/codeodor/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-01-03T16:01:52Z","updated_at":"2015-01-03T16:40:40Z","closed_at":null,"body":"Suppose you have\r\n\r\n```ruby\r\nclass Season < ActiveRecord::Base\r\n  belongs_to :division\r\n  has_one :champion\r\nend\r\n\r\nclass Division < ActiveRecord::Base\r\nend\r\n\r\nclass Champion < ActiveRecord::Base\r\nend\r\n```\r\n\r\nThen we have this code that uses it:\r\n\r\n```ruby\r\nseasons = Season.includes(:division, :champion).references(:champion)\r\nseasons.each{|s| puts s.champion.inspect }\r\n```\r\n\r\nThis will run a query for each `champion` if `champion` is `nil` starting in Rails 4.1.\r\n\r\nIf you remove the call to `references` however, it behaves as expected."},"comment":{"url":"https://api.github.com/repos/rails/rails/issues/comments/68600864","html_url":"https://github.com/rails/rails/issues/18312#issuecomment-68600864","issue_url":"https://api.github.com/repos/rails/rails/issues/18312","id":68600864,"user":{"login":"codeodor","id":23479,"avatar_url":"https://avatars.githubusercontent.com/u/23479?v=3","gravatar_id":"","url":"https://api.github.com/users/codeodor","html_url":"https://github.com/codeodor","followers_url":"https://api.github.com/users/codeodor/followers","following_url":"https://api.github.com/users/codeodor/following{/other_user}","gists_url":"https://api.github.com/users/codeodor/gists{/gist_id}","starred_url":"https://api.github.com/users/codeodor/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/codeodor/subscriptions","organizations_url":"https://api.github.com/users/codeodor/orgs","repos_url":"https://api.github.com/users/codeodor/repos","events_url":"https://api.github.com/users/codeodor/events{/privacy}","received_events_url":"https://api.github.com/users/codeodor/received_events","type":"User","site_admin":false},"created_at":"2015-01-03T16:40:40Z","updated_at":"2015-01-03T16:40:40Z","body":"If I change what I said above to not set the target to nil (i.e. just do `ar_parent.association(node.reflection.name)`, it seems to fix the issue a little better, but there are extra items returned for some of the eager loading test cases which are collections."}}