Skip to content

Better detection of standard preload #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/active_record/virtual_attributes/virtual_fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,12 @@ def call

# convert the includes with virtual attributes to includes with proper associations
records_by_assoc = records.group_by { |rec| assoc_cache[rec.class] }
# if these are the same includes, then do the preloader work
return super if records_by_assoc.size == 1 && records_by_assoc.keys.first == associations
# If the association were already translated, then short circuit / do the standard preloader work.
# When replace_virtual_fields removes the outer array, match that too.
if records_by_assoc.size == 1 &&
(associations == records_by_assoc.keys.first || associations == [records_by_assoc.keys.first])
return super
end

# for each of the associations, run a preloader
records_by_assoc.each do |klass_associations, klass_records|
Expand Down
49 changes: 39 additions & 10 deletions spec/virtual_includes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,6 @@
expect(Author.includes(:books => :author_name).references(:books)).to preload_values(:first_book_author_name, author_name)
expect(Author.includes(:books => [:author_name]).references(:books)).to preload_values(:first_book_author_name, author_name)
expect(Author.includes(:books => {:author_name => {}}).references(:books)).to preload_values(:first_book_author_name, author_name)
end

it "uses preloaded fields" do
expect(Author.includes(:books => :author_name).references(:books)).to preload_values(:first_book_author_name, author_name)
expect(Author.includes(:books => [:author_name]).references(:books)).to preload_values(:first_book_author_name, author_name)
expect(Author.includes(:books => {:author_name => {}}).references(:books)).to preload_values(:first_book_author_name, author_name)
inc = Author.virtual_includes(:first_book_author_name)
expect(Author.includes(inc).references(:books)).to preload_values(:first_book_author_name, author_name)
end
Expand Down Expand Up @@ -310,10 +304,6 @@
it "uses included fields" do
expect(preloaded(Author.all.to_a, :books => :author_name)).to preload_values(:first_book_author_name, author_name)
end

it "uses preloaded fields" do
expect(preloaded(Author.all.to_a, :books => :author_name)).to preload_values(:first_book_author_name, author_name)
end
end

context "preloads virtual_reflection with includes" do
Expand Down Expand Up @@ -388,11 +378,50 @@
end
end

describe ".eager_load" do
it "preloads standard associations (:books)" do
expect(Author.eager_load(:books)).to preload_values(:first_book_name, book_name)
expect(Author.eager_load([:books])).to preload_values(:first_book_name, book_name)
expect(Author.eager_load([[:books]])).to preload_values(:first_book_name, book_name)
expect(Author.eager_load(:books => {})).to preload_values(:first_book_name, book_name)
end

it "preloads associations (:uses => :books)" do
expect(Author.eager_load(:first_book_name)).to preload_values(:first_book_name, book_name)
expect(Author.eager_load([:first_book_name])).to preload_values(:first_book_name, book_name)
expect(Author.eager_load([[:first_book_name]])).to preload_values(:first_book_name, book_name)
expect(Author.eager_load(:first_book_name => {})).to preload_values(:first_book_name, book_name)
end
end

describe ".preload" do
it "preloads standard associations (:books)" do
expect(Author.preload(:books)).to preload_values(:first_book_name, book_name)
expect(Author.preload([:books])).to preload_values(:first_book_name, book_name)
expect(Author.preload([[:books]])).to preload_values(:first_book_name, book_name)
expect(Author.preload(:books => {})).to preload_values(:first_book_name, book_name)
end

it "preloads associations (:uses => :books)" do
expect(Author.preload(:first_book_name)).to preload_values(:first_book_name, book_name)
expect(Author.preload([:first_book_name])).to preload_values(:first_book_name, book_name)
expect(Author.preload([[:first_book_name]])).to preload_values(:first_book_name, book_name)
expect(Author.preload(:first_book_name => {})).to preload_values(:first_book_name, book_name)
end
end

context "preloads virtual_reflection with preloader" do
it "preloads virtual_reflection (:uses => :books)" do
expect(preloaded(Author.all.to_a, :named_books)).to preload_values(:named_books, named_books)
end

it "preloads virtual_reflection ([:books])" do
expect(preloaded(Author.all.to_a, :books)).to preload_values(:named_books, named_books)
# NOTE: the next test shows a double preloader call
expect(preloaded(Author.all.to_a, [:books])).to preload_values(:named_books, named_books)
expect(preloaded(Author.all.to_a, :books => {})).to preload_values(:named_books, named_books)
end

it "preloads virtual_reflection (:uses => {:books => :author_name})" do
expect(preloaded(Author.all.to_a, :books_with_authors)).to preload_values(:books_with_authors, named_books)
end
Expand Down