Skip to content

Commit 55e76f1

Browse files
committed
Remove set_new_record from singular association creation
1 parent 14b1d73 commit 55e76f1

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

activerecord/lib/active_record/associations/singular_association.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ def _create_record(attributes, raise_error = false, &block)
5757
reflection.klass.transaction do
5858
record = build(attributes, &block)
5959
saved = record.save
60-
set_new_record(record)
6160
raise RecordInvalid.new(record) if !saved && raise_error
6261
record
6362
end

activerecord/test/cases/associations/belongs_to_associations_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,6 +1428,22 @@ def test_should_set_foreign_key_on_create_association!
14281428
assert_equal firm.id, client.client_of
14291429
end
14301430

1431+
def test_should_set_foreign_key_on_save
1432+
client = Client.create! name: "fuu"
1433+
firm = client.build_firm name: "baa"
1434+
1435+
firm.save
1436+
assert_equal firm.id, client.client_of
1437+
end
1438+
1439+
def test_should_set_foreign_key_on_save!
1440+
client = Client.create! name: "fuu"
1441+
firm = client.build_firm name: "baa"
1442+
1443+
firm.save!
1444+
assert_equal firm.id, client.client_of
1445+
end
1446+
14311447
def test_self_referential_belongs_to_with_counter_cache_assigning_nil
14321448
comment = Comment.create! post: posts(:thinking), body: "fuu"
14331449
comment.parent = nil

activerecord/test/models/company.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ class Firm < Company
8484
has_one :account_with_inexistent_foreign_key, class_name: "Account", foreign_key: "inexistent"
8585
has_one :deletable_account, foreign_key: "firm_id", class_name: "Account", dependent: :delete
8686

87+
has_one :client, foreign_key: "client_of"
88+
8789
has_one :account_limit_500_with_hash_conditions, -> { where credit_limit: 500 }, foreign_key: "firm_id", class_name: "Account"
8890

8991
has_one :unautosaved_account, foreign_key: "firm_id", class_name: "Account", autosave: false
@@ -134,7 +136,7 @@ class Agency < Firm
134136
end
135137

136138
class Client < Company
137-
belongs_to :firm, foreign_key: "client_of"
139+
belongs_to :firm, foreign_key: "client_of", inverse_of: :client
138140
belongs_to :firm_with_basic_id, class_name: "Firm", foreign_key: "firm_id"
139141
belongs_to :firm_with_select, -> { select("id") }, class_name: "Firm", foreign_key: "firm_id"
140142
belongs_to :firm_with_other_name, class_name: "Firm", foreign_key: "client_of"

0 commit comments

Comments
 (0)