aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/models
diff options
context:
space:
mode:
authorSam DeCesare <sam@samdecesare.com>2018-04-09 17:35:33 -0700
committerSam DeCesare <sam@samdecesare.com>2018-04-09 18:49:52 -0700
commit99910dddf28faac31d6a3d4800460f1bc308bb83 (patch)
tree4b08bd565a1b2d901bd1e3b5af2ea5b5537b1881 /activerecord/test/models
parent601e31b362b2f59bed988de716f4b931b2aa0950 (diff)
downloadrails-99910dddf28faac31d6a3d4800460f1bc308bb83.tar.gz
rails-99910dddf28faac31d6a3d4800460f1bc308bb83.tar.bz2
rails-99910dddf28faac31d6a3d4800460f1bc308bb83.zip
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.
Diffstat (limited to 'activerecord/test/models')
-rw-r--r--activerecord/test/models/game.rb7
-rw-r--r--activerecord/test/models/game_board.rb7
-rw-r--r--activerecord/test/models/game_collection.rb5
-rw-r--r--activerecord/test/models/game_owner.rb5
4 files changed, 24 insertions, 0 deletions
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