aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2014-06-14 09:21:31 -0600
committerSean Griffin <sean@thoughtbot.com>2014-06-17 10:27:02 -0600
commit068f092ced8483e557725542dd919ab7c516e567 (patch)
tree26a8fb8e3776fef2a6133b537060742605c09008 /activerecord/test
parente7a8fda0336fe733adbdd0bdb642a09327ef22f1 (diff)
downloadrails-068f092ced8483e557725542dd919ab7c516e567.tar.gz
rails-068f092ced8483e557725542dd919ab7c516e567.tar.bz2
rails-068f092ced8483e557725542dd919ab7c516e567.zip
Don't save through records twice
If the through record gets created in an `after_create` hook that is defined before the association is defined (therefore after its `after_create` hook) get saved twice. This ensures that the through records are created only once, regardless of the order of the hooks.
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/associations/has_many_through_associations_test.rb19
1 files changed, 19 insertions, 0 deletions
diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb
index 8641584c0c..6895df73c1 100644
--- a/activerecord/test/cases/associations/has_many_through_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb
@@ -1139,4 +1139,23 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase
assert_equal 2, post.lazy_readers_unscope_skimmers.to_a.size
assert_equal 2, post.lazy_people_unscope_skimmers.to_a.size
end
+
+ class ClubWithCallbacks < ActiveRecord::Base
+ self.table_name = 'clubs'
+ after_create :add_a_member
+
+ has_many :memberships, inverse_of: :club, foreign_key: :club_id
+ has_many :members, through: :memberships
+
+ def add_a_member
+ members << Member.last
+ end
+ end
+
+ def test_has_many_with_callback_before_association
+ Member.create!
+ club = ClubWithCallbacks.create!
+
+ assert_equal 1, club.reload.memberships.count
+ end
end