rails/rails — pull request #8674 by senny (2013-01-01 16:34:05+00:00)
Title: test case to illustrate callbacks definitions and HABTM association are order dependent 

WIP

The source issue is: #8672

This PR is only a failing test case as I am not completely sure how to tackle the problem.

I investigated and can confirm that the order in which `after_create` and `has_and_belongs_to_many` associations are defined does impact the functionality.

If the callback is defined before the association every HABTM record created inside the callback will be inserted twice. The reason is `AutosaveAssociation#save_collection_association`. It will pick up the already created records and save them again. This happens because `@new_record_before_save` will be true and since it's an `after_create` callback, the associations are inserted directly with `<<`.

My test case results in the following failure:

```
1) Failure:
test_after_create_before_habtm_definition(HasAndBelongsToManyAssociationsTest) [test/cases/associations/has_and_belongs_to_many_associations_test.rb:616]:
Expected: 1
Actual: 2
```