From 99910dddf28faac31d6a3d4800460f1bc308bb83 Mon Sep 17 00:00:00 2001 From: Sam DeCesare Date: Mon, 9 Apr 2018 17:35:33 -0700 Subject: Fix .new with multiple through associations This fixes a bug with building an object that has multiple `has_many :through` associations through the same object. Previously, when building the object via .new, the intermediate object would be created instead of just being built. Here's an example: Given a GameBoard, that has_one Owner and Collection through Game. The following line would cause a game object to be created in the database. GameBoard.new(owner: some_owner, collection: some_collection) Whereas, if passing only one of those associations into `.new` would cause the Game object to be built and not created in the database. Now the above code will only build the Game object, and not save it. --- .../associations/has_one_through_association.rb | 6 +++++- .../has_one_through_associations_test.rb | 22 ++++++++++++++++++++++ activerecord/test/models/game.rb | 7 +++++++ activerecord/test/models/game_board.rb | 7 +++++++ activerecord/test/models/game_collection.rb | 5 +++++ activerecord/test/models/game_owner.rb | 5 +++++ activerecord/test/schema/schema.rb | 13 +++++++++++++ 7 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 activerecord/test/models/game.rb create mode 100644 activerecord/test/models/game_board.rb create mode 100644 activerecord/test/models/game_collection.rb create mode 100644 activerecord/test/models/game_owner.rb diff --git a/activerecord/lib/active_record/associations/has_one_through_association.rb b/activerecord/lib/active_record/associations/has_one_through_association.rb index 491282adf7..019bf0729f 100644 --- a/activerecord/lib/active_record/associations/has_one_through_association.rb +++ b/activerecord/lib/active_record/associations/has_one_through_association.rb @@ -28,7 +28,11 @@ module ActiveRecord end if through_record - through_record.update(attributes) + if through_record.new_record? + through_record.assign_attributes(attributes) + else + through_record.update(attributes) + end elsif owner.new_record? || !save through_proxy.build(attributes) else 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 9964f084ac..003b82e50e 100644 --- a/activerecord/test/cases/associations/has_one_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_through_associations_test.rb @@ -22,6 +22,10 @@ require "models/customer" require "models/carrier" require "models/shop_account" require "models/customer_carrier" +require "models/game" +require "models/game_board" +require "models/game_collection" +require "models/game_owner" class HasOneThroughAssociationsTest < ActiveRecord::TestCase fixtures :member_types, :members, :clubs, :memberships, :sponsors, :organizations, :minivans, @@ -64,6 +68,24 @@ class HasOneThroughAssociationsTest < ActiveRecord::TestCase assert_equal clubs(:moustache_club), new_member.club end + def test_building_multiple_associations_builds_through_record + game_owner = GameOwner.create + game_collection = GameCollection.create + game_board_with_one_association = GameBoard.new(game_owner: game_owner) + assert_nil game_board_with_one_association.game.id + game_board_with_two_associations = GameBoard.new(game_owner: game_owner, game_collection: game_collection) + assert_nil game_board_with_two_associations.game.id + end + + def test_creating_multiple_associations_creates_through_record + game_owner = GameOwner.create + game_collection = GameCollection.create + game_board_with_one_association = GameBoard.create(game_owner: game_owner) + assert_not_nil game_board_with_one_association.game.id + game_board_with_two_associations = GameBoard.create(game_owner: game_owner, game_collection: game_collection) + assert_not_nil game_board_with_two_associations.game.id + end + def test_creating_association_sets_both_parent_ids_for_new member = Member.new(name: "Sean Griffin") club = Club.new(name: "Da Club") diff --git a/activerecord/test/models/game.rb b/activerecord/test/models/game.rb new file mode 100644 index 0000000000..0c33075b12 --- /dev/null +++ b/activerecord/test/models/game.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class Game < ActiveRecord::Base + has_one :game_board + belongs_to :game_owner + belongs_to :game_collection +end diff --git a/activerecord/test/models/game_board.rb b/activerecord/test/models/game_board.rb new file mode 100644 index 0000000000..01d082eddb --- /dev/null +++ b/activerecord/test/models/game_board.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class GameBoard < ActiveRecord::Base + belongs_to :game + has_one :game_owner, through: :game + has_one :game_collection, through: :game +end diff --git a/activerecord/test/models/game_collection.rb b/activerecord/test/models/game_collection.rb new file mode 100644 index 0000000000..853876b0bd --- /dev/null +++ b/activerecord/test/models/game_collection.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +class GameCollection < ActiveRecord::Base + has_many :games +end diff --git a/activerecord/test/models/game_owner.rb b/activerecord/test/models/game_owner.rb new file mode 100644 index 0000000000..82be3e75b2 --- /dev/null +++ b/activerecord/test/models/game_owner.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +class GameOwner < ActiveRecord::Base + has_many :games +end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 350113eaab..274879d4af 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -364,6 +364,19 @@ ActiveRecord::Schema.define do t.integer :follower_id end + create_table :games, force: true do |t| + t.integer :game_owner_id + t.integer :game_collection_id + end + + create_table :game_boards, force: true do |t| + t.integer :game_id + end + + create_table :game_collections, force: true + + create_table :game_owners, force: true + create_table :goofy_string_id, force: true, id: false do |t| t.string :id, null: false t.string :info -- cgit v1.2.3