-
Notifications
You must be signed in to change notification settings - Fork 21.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46383 from dmytro-savochkin/fix-activerecord-relo…
…ad-association-cache Ensure `reload` sets correct owner for each association (ActiveRecord::Persistence)
- Loading branch information
1 parent
81c8023
commit 57a50f5
Showing
7 changed files
with
67 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
activerecord/test/cases/persistence/reload_association_cache_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
require "cases/helper" | ||
require "models/publication" | ||
require "models/editorship" | ||
require "models/editor" | ||
|
||
class ReloadAssociationCacheTest < ActiveRecord::TestCase | ||
def test_reload_sets_correct_owner_for_association_cache | ||
publication = Publication.create!(name: "Rails Way") | ||
assert_equal "Rails Way (touched)", publication.name | ||
publication.reload | ||
assert_equal "Rails Way", publication.name | ||
publication.transaction do | ||
publication.editors = [publication.build_editor_in_chief(name: "Alex Black")] | ||
publication.save! | ||
end | ||
assert_equal "Rails Way (touched)", publication.name | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
class Editor < ActiveRecord::Base | ||
self.primary_key = "name" | ||
|
||
has_one :publication, foreign_key: :editor_in_chief_id, inverse_of: :editor_in_chief | ||
has_many :editorships | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
class Editorship < ActiveRecord::Base | ||
belongs_to :publication | ||
belongs_to :editor | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
class Publication < ActiveRecord::Base | ||
belongs_to :editor_in_chief, class_name: "Editor", inverse_of: :publication, optional: true | ||
has_many :editorships | ||
has_many :editors, through: :editorships | ||
|
||
after_initialize do | ||
self.editor_in_chief = build_editor_in_chief(name: "John Doe") | ||
end | ||
|
||
after_save_commit :touch_name | ||
def touch_name | ||
self.name = "#{name} (touched)" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters