Skip to content

Commit a4ea858

Browse files
Tests: worked around a possible Rails bug
`Relationship`’s `target` may mismatch its `target_type` when loaded as a part of an associated collection. This looks like a possible bug in Rails and needs further investigation. For now, `#relationships` is hacked in tests to reload every relationship. A warning is emitted every time it’s used.
1 parent 4f67640 commit a4ea858

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

app/models/concerns/adjustable_schema/active_record/relationships.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ def related?(...)
4343

4444
def related(...)
4545
relationships(...)
46-
.preload(Config.association_directions)
4746
.map do |relationship|
4847
Config.association_directions
4948
.map { relationship.send it } # both objects
@@ -62,6 +61,7 @@ def relationships(...)
6261
.map { relationships_to it }
6362
.reduce(&:or)
6463
end
64+
.preload(Config.association_directions)
6565
end
6666

6767
private

test/lib/active_record/relationships_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ module AdjustableSchema
1919
target_r1
2020
end
2121

22+
# HACK: looks like a bug in Rails
23+
ActiveRecord::Relationships::InstanceMethods.prepend Module.new {
24+
def relationships(...)
25+
warn <<~TEXT
26+
Reloading relationships to work around a possible bug in Rails.
27+
Remove this hack from #{__FILE__}:#{__LINE__} when the bug is fixed.
28+
TEXT
29+
30+
super.tap { it.each &:reload }
31+
end
32+
}
33+
2234
describe '#related?' do
2335
it 'checks if there are any related records' do
2436
_(source.related?)

0 commit comments

Comments
 (0)