Skip to content

Commit 25b9b53

Browse files
authored
Merge pull request #195 from kbrock/poly_nil_uses_blowup
Fix nil :uses issue when included through a polymorphic
2 parents d6a3c90 + 418e8e0 commit 25b9b53

File tree

5 files changed

+21
-2
lines changed

5 files changed

+21
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ The versioning of this gem follows ActiveRecord versioning, and does not follow
44

55
## [Unreleased]
66

7+
## [7.2.0.1] - 2025-07-18
8+
9+
* Fix includes and associations with empty uses [#195](https://github.com/ManageIQ/activerecord-virtual_attributes/pull/195)
10+
711
## [7.2.0.0] - 2025-06-25
812

913
* virtual_delegate requires type [#185](https://github.com/ManageIQ/activerecord-virtual_attributes/pull/185)
@@ -125,7 +129,8 @@ The versioning of this gem follows ActiveRecord versioning, and does not follow
125129
* Initial Release
126130
* Extracted from ManageIQ/manageiq
127131

128-
[Unreleased]: https://github.com/ManageIQ/activerecord-virtual_attributes/compare/v7.2.0.0...HEAD
132+
[Unreleased]: https://github.com/ManageIQ/activerecord-virtual_attributes/compare/v7.2.0.1...HEAD
133+
[7.2.0.1]: https://github.com/ManageIQ/activerecord-virtual_attributes/compare/v7.2.0.0...v7.2.0.1
129134
[7.2.0.0]: https://github.com/ManageIQ/activerecord-virtual_attributes/compare/v7.1.2...v7.2.0.0
130135
[7.1.2]: https://github.com/ManageIQ/activerecord-virtual_attributes/compare/v7.1.1...v7.1.2
131136
[7.1.1]: https://github.com/ManageIQ/activerecord-virtual_attributes/compare/v7.1.0...v7.1.1
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module ActiveRecord
22
module VirtualAttributes
3-
VERSION = "7.2.0.0".freeze
3+
VERSION = "7.2.0.1".freeze
44
end
55
end

lib/active_record/virtual_attributes/virtual_fields.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ def grouped_records
183183
source_records.each do |record|
184184
# begin virtual_attributes changes
185185
association = record.class.replace_virtual_fields(self.association)
186+
next if association.nil?
186187
# end virtual_attributes changes
187188

188189
reflection = record.class._reflect_on_association(association)

spec/db/models.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ def book_with_most_bookmarks
115115
# vs a more condensed format: {:bwmb => {}, :books => co_a}
116116
virtual_has_many :famous_co_authors, :uses => [:book_with_most_bookmarks, {:books => :co_authors}]
117117

118+
virtual_has_many :thoughts
119+
120+
def thoughts
121+
["one", "two"]
122+
end
123+
118124
def self.create_with_books(count)
119125
create!(:name => "foo", :blurb => "blah blah blah").tap { |author| author.create_books(count) }
120126
end

spec/virtual_includes_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@
106106
it "counts" do
107107
expect { expect(Author.includes(:books => :author_name).count).to eq(1) }.not_to raise_error
108108
end
109+
110+
# we are trying to get the includes with a nil uses to blow things up
111+
it "preloads through polymorphic to an association without a uses" do
112+
author = Author.first
113+
photo = author.photos.create
114+
expect(author.photos.includes(:imageable => :thoughts).first).to eq(photo)
115+
end
109116
end
110117

111118
# references follow a different path than just includes

0 commit comments

Comments
 (0)