{"action":"created","issue":{"url":"https://api.github.com/repos/rails/rails/issues/17599","labels_url":"https://api.github.com/repos/rails/rails/issues/17599/labels{/name}","comments_url":"https://api.github.com/repos/rails/rails/issues/17599/comments","events_url":"https://api.github.com/repos/rails/rails/issues/17599/events","html_url":"https://github.com/rails/rails/issues/17599","id":48559831,"number":17599,"title":"Two association records created for has_many through","user":{"login":"daveroberts","id":42104,"avatar_url":"https://avatars.githubusercontent.com/u/42104?v=3","gravatar_id":"","url":"https://api.github.com/users/daveroberts","html_url":"https://github.com/daveroberts","followers_url":"https://api.github.com/users/daveroberts/followers","following_url":"https://api.github.com/users/daveroberts/following{/other_user}","gists_url":"https://api.github.com/users/daveroberts/gists{/gist_id}","starred_url":"https://api.github.com/users/daveroberts/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/daveroberts/subscriptions","organizations_url":"https://api.github.com/users/daveroberts/orgs","repos_url":"https://api.github.com/users/daveroberts/repos","events_url":"https://api.github.com/users/daveroberts/events{/privacy}","received_events_url":"https://api.github.com/users/daveroberts/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/rails/rails/labels/activerecord","name":"activerecord","color":"0b02e1"},{"url":"https://api.github.com/repos/rails/rails/labels/With%20reproduction%20steps","name":"With reproduction steps","color":"009800"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2014-11-12T20:42:49Z","updated_at":"2015-12-30T20:22:21Z","closed_at":null,"body":"I'd like to revisit issue #3798.  In this issue, @rafaelfranca offers the following recommendation\r\n\r\n> I don't think this is an issue. Associations defines callbacks. Callback order is important since they are executed in the correct order. That said you should not place callbacks before the associations. This is even [documented](https://github.com/rails/rails/blob/31bfcdc77ca0d8cec9b5fe513bdc6f05814dd4f1/activerecord/lib/active_record/associations.rb#L1222-1227)\r\n\r\nI believe I have come up with an example where the callback is defined after the associations as Rafael recommends, but the problem is still occurring.  In this case, is there a valid workaround, or is this buggy behavior?\r\n\r\n```ruby\r\nclass Audit < ActiveRecord::Base\r\nend\r\nclass AuditAssignment < ActiveRecord::Base\r\n  belongs_to :audit\r\n  belongs_to :item, polymorphic: true\r\nend\r\nclass Group < ActiveRecord::Base\r\n  has_many :roles\r\n  has_many :users, through: :roles\r\nend\r\nclass Role < ActiveRecord::Base\r\n  belongs_to :user\r\n  belongs_to :group\r\n\r\n  after_create :audit_creation\r\n  def audit_creation\r\n    a = Audit.new(message: \"User added to group\")\r\n    self.user.audits << a\r\n  end\r\nend\r\nclass User < ActiveRecord::Base\r\n  has_many :roles\r\n  has_many :groups, through: :roles\r\n  has_many :audits, through: :audit_assignments\r\n  has_many :audit_assignments, -> {where(item_type: 'User')}, foreign_key: :item_id\r\nend\r\n```\r\n\r\nGiven this model, the following test will fail:\r\n\r\n```ruby\r\ntest \"creates only one audit assignment\" do\r\n    g = Group.create(name: \"Administrators\")\r\n    u = User.new(name: \"Admin\", groups: [g])\r\n    u.save\r\n    assert AuditAssignment.count == 1\r\n  end\r\n```\r\n\r\nNotice the after_create hook of AuditAssignment is placed after all associations in that class.\r\n\r\nI have reproduced the problem in a [minimal Rails project](https://github.com/daveroberts/double_user_group), based on 4.1.7, that has a single failing test."},"comment":{"url":"https://api.github.com/repos/rails/rails/issues/comments/168067993","html_url":"https://github.com/rails/rails/issues/17599#issuecomment-168067993","issue_url":"https://api.github.com/repos/rails/rails/issues/17599","id":168067993,"user":{"login":"tarasmatsyk","id":4949627,"avatar_url":"https://avatars.githubusercontent.com/u/4949627?v=3","gravatar_id":"","url":"https://api.github.com/users/tarasmatsyk","html_url":"https://github.com/tarasmatsyk","followers_url":"https://api.github.com/users/tarasmatsyk/followers","following_url":"https://api.github.com/users/tarasmatsyk/following{/other_user}","gists_url":"https://api.github.com/users/tarasmatsyk/gists{/gist_id}","starred_url":"https://api.github.com/users/tarasmatsyk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tarasmatsyk/subscriptions","organizations_url":"https://api.github.com/users/tarasmatsyk/orgs","repos_url":"https://api.github.com/users/tarasmatsyk/repos","events_url":"https://api.github.com/users/tarasmatsyk/events{/privacy}","received_events_url":"https://api.github.com/users/tarasmatsyk/received_events","type":"User","site_admin":false},"created_at":"2015-12-30T20:22:21Z","updated_at":"2015-12-30T20:22:21Z","body":"@rafaelfranca, yes, the bug is still persist in rails 4.2\r\n"}}