diff options
author | Sean Griffin <sean@thoughtbot.com> | 2014-06-08 16:04:03 -0600 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2014-06-13 14:38:34 -0600 |
commit | 0a4e3f4a7f6d93c713389140bc6e6613b721b0ff (patch) | |
tree | 393f55354f3eb63dde582612a7b26dd5e929ffbf /activerecord/test | |
parent | 49fee3d271e52a44a7bc7fcbbcb00792b613b7df (diff) | |
download | rails-0a4e3f4a7f6d93c713389140bc6e6613b721b0ff.tar.gz rails-0a4e3f4a7f6d93c713389140bc6e6613b721b0ff.tar.bz2 rails-0a4e3f4a7f6d93c713389140bc6e6613b721b0ff.zip |
Through associations should set both parent ids on join models
member = Member.new(club: Club.new)
member.save!
Before:
member.current_membership.club_id # => nil
After:
member.current_membership.club_id # => club's id
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/associations/has_many_through_associations_test.rb | 13 | ||||
-rw-r--r-- | activerecord/test/cases/associations/has_one_through_associations_test.rb | 14 |
2 files changed, 27 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 2e62189e7a..8641584c0c 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -330,6 +330,19 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase assert post.single_people.include?(person) end + def test_both_parent_ids_set_when_saving_new + post = Post.new(title: 'Hello', body: 'world') + person = Person.new(first_name: 'Sean') + + post.people = [person] + post.save + + assert post.id + assert person.id + assert_equal post.id, post.readers.first.post_id + assert_equal person.id, post.readers.first.person_id + end + def test_delete_association assert_queries(2){posts(:welcome);people(:michael); } diff --git a/activerecord/test/cases/associations/has_one_through_associations_test.rb b/activerecord/test/cases/associations/has_one_through_associations_test.rb index a2725441b3..089cb0a3a2 100644 --- a/activerecord/test/cases/associations/has_one_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_through_associations_test.rb @@ -45,6 +45,20 @@ class HasOneThroughAssociationsTest < ActiveRecord::TestCase assert_equal clubs(:moustache_club), new_member.club end + def test_creating_association_sets_both_parent_ids_for_new + member = Member.new(name: 'Sean Griffin') + club = Club.new(name: 'Da Club') + + member.club = club + + member.save! + + assert member.id + assert club.id + assert_equal member.id, member.current_membership.member_id + assert_equal club.id, member.current_membership.club_id + end + def test_replace_target_record new_club = Club.create(:name => "Marx Bros") @member.club = new_club |