{"action":"created","issue":{"url":"https://api.github.com/repos/rails/rails/issues/18988","labels_url":"https://api.github.com/repos/rails/rails/issues/18988/labels{/name}","comments_url":"https://api.github.com/repos/rails/rails/issues/18988/comments","events_url":"https://api.github.com/repos/rails/rails/issues/18988/events","html_url":"https://github.com/rails/rails/issues/18988","id":58072495,"number":18988,"title":"Bug in belongs_to when using counter_cache together with foreign_key","user":{"login":"arnodirlam","id":43364,"avatar_url":"https://avatars.githubusercontent.com/u/43364?v=3","gravatar_id":"","url":"https://api.github.com/users/arnodirlam","html_url":"https://github.com/arnodirlam","followers_url":"https://api.github.com/users/arnodirlam/followers","following_url":"https://api.github.com/users/arnodirlam/following{/other_user}","gists_url":"https://api.github.com/users/arnodirlam/gists{/gist_id}","starred_url":"https://api.github.com/users/arnodirlam/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/arnodirlam/subscriptions","organizations_url":"https://api.github.com/users/arnodirlam/orgs","repos_url":"https://api.github.com/users/arnodirlam/repos","events_url":"https://api.github.com/users/arnodirlam/events{/privacy}","received_events_url":"https://api.github.com/users/arnodirlam/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/rails/rails/labels/stale","name":"stale","color":"444444"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-02-18T14:14:55Z","updated_at":"2015-06-26T02:35:15Z","closed_at":null,"body":"Hi,\r\n\r\nwhen I use belongs_to with both counter_cache and foreign_key it doesn't update the counter cache correctly, since the update_counters method always operates on the table's primary key, instead of the foreign_key relevant in this case - as can be seen [here](https://github.com/rails/rails/blob/1152219fa2d7968f864b3a5ebb78d2f95ca4fb5c/activerecord/lib/active_record/counter_cache.rb#L83).\r\n\r\nI guess what could be done is to add a parameter holding the column that id references, which evaluates to the table's primary_key by default. This would have to be passed on by belongs_to when using increment_counter and decrement_counter [here](https://github.com/rails/rails/blob/78dab2a8569408658542e462a957ea5a35aa4679/activerecord/lib/active_record/associations/builder/belongs_to.rb#L42).\r\n\r\nShould I try to make a pull request (would be my first to rails)? If yes, could someone please point me to file(s) appropriate fro the tests?\r\n\r\nCheers, Arno"},"comment":{"url":"https://api.github.com/repos/rails/rails/issues/comments/115469352","html_url":"https://github.com/rails/rails/issues/18988#issuecomment-115469352","issue_url":"https://api.github.com/repos/rails/rails/issues/18988","id":115469352,"user":{"login":"rails-bot","id":7468109,"avatar_url":"https://avatars.githubusercontent.com/u/7468109?v=3","gravatar_id":"","url":"https://api.github.com/users/rails-bot","html_url":"https://github.com/rails-bot","followers_url":"https://api.github.com/users/rails-bot/followers","following_url":"https://api.github.com/users/rails-bot/following{/other_user}","gists_url":"https://api.github.com/users/rails-bot/gists{/gist_id}","starred_url":"https://api.github.com/users/rails-bot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rails-bot/subscriptions","organizations_url":"https://api.github.com/users/rails-bot/orgs","repos_url":"https://api.github.com/users/rails-bot/repos","events_url":"https://api.github.com/users/rails-bot/events{/privacy}","received_events_url":"https://api.github.com/users/rails-bot/received_events","type":"User","site_admin":false},"created_at":"2015-06-26T02:35:15Z","updated_at":"2015-06-26T02:35:15Z","body":"This issue has been automatically marked as stale because it has not been commented on for at least\nthree months.\n\nThe resources of the Rails team are limited, and so we are asking for your help.\n\nIf you can still reproduce this error on the `4-2-stable`, `4-1-stable` branches or on `master`,\nplease reply with all of the information you have about it in order to keep the issue open.\n\nThank you for all your contributions.\n"}}