Skip to content

Commit 90e5a56

Browse files
committed
Fix bug with reading dirty attributes on namespaced classes
When a class' name does not match its STI name, ActiveRecord will reload an associated record from the database even if it's already in memory. This bug exists in Rails 4 and above.
1 parent bb9b73c commit 90e5a56

3 files changed

Lines changed: 24 additions & 3 deletions

File tree

lib/polymorphic_integer_type.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require "polymorphic_integer_type/version"
44
require "polymorphic_integer_type/extensions"
55
require "polymorphic_integer_type/mapping"
6+
require "polymorphic_integer_type/belongs_to_polymorphic_association_extension"
67

78
if ACTIVE_RECORD_VERSION < Gem::Version.new("5")
89
require "polymorphic_integer_type/activerecord_4/predicate_builder_extension"
@@ -14,4 +15,5 @@
1415
require "polymorphic_integer_type/activerecord_5_0_0/association_query_handler_extension"
1516
end
1617

18+
1719
module PolymorphicIntegerType; end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module ActiveRecord
2+
module Associations
3+
class BelongsToPolymorphicAssociation < BelongsToAssociation
4+
private def replace_keys(record)
5+
super
6+
owner[reflection.foreign_type] = record.class.base_class
7+
end
8+
end
9+
end
10+
end

spec/polymorphic_integer_type_spec.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@
2929
link = Link.new(source: Namespaced::Animal.new)
3030
expect(link.source_type).to eql "Animal"
3131
end
32+
33+
it "can read dirty attributes from an associated object" do
34+
animal = Namespaced::Animal.create!(name: "Oldie")
35+
animal.name = "Newton"
36+
link = Link.create!(source: animal)
37+
38+
expect(link.source.name).to eq("Newton")
39+
end
3240
end
3341

3442
context "when querying the associations" do
@@ -109,7 +117,7 @@
109117

110118
end
111119

112-
context "When using a relation to the links with eagar loading" do
120+
context "When using a relation to the links with eager loading" do
113121
let!(:links){
114122
[Link.create(source: source, target: kibble),
115123
Link.create(source: source, target: water)]
@@ -124,7 +132,7 @@
124132

125133
end
126134

127-
context "When using a through relation to the links with eagar loading" do
135+
context "When using a through relation to the links with eager loading" do
128136
let!(:links){
129137
[Link.create(source: source, target: kibble),
130138
Link.create(source: source, target: water)]
@@ -139,7 +147,7 @@
139147

140148
end
141149

142-
context "When eagar loading the polymorphic association" do
150+
context "When eager loading the polymorphic association" do
143151
let(:link) { Link.create(source_id: source.id, source_type: source.class.to_s) }
144152
let(:source) { cat }
145153

@@ -160,6 +168,7 @@
160168
end
161169

162170

171+
163172
end
164173

165174
context "when the association is an STI table" do

0 commit comments

Comments
 (0)