Skip to content

Make sure we correctly find the association within preload_scoped_relation method #75

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
Nov 4, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ Gemfile.lock

# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc
.ruby-version
2 changes: 2 additions & 0 deletions lib/jit_preloader/active_record/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def clear_jit_preloader!
def preload_scoped_relation(name:, base_association:, preload_scope: nil)
return jit_preload_scoped_relations[name] if jit_preload_scoped_relations&.key?(name)

base_association = base_association.to_sym
records = jit_preloader&.records || [self]
previous_association_values = {}

Expand Down Expand Up @@ -55,6 +56,7 @@ def preload_scoped_relation(name:, base_association:, preload_scope: nil)
def preload_scoped_relation(name:, base_association:, preload_scope: nil)
return jit_preload_scoped_relations[name] if jit_preload_scoped_relations&.key?(name)

base_association = base_association.to_sym
records = jit_preloader&.records || [self]
previous_association_values = {}

Expand Down
2 changes: 1 addition & 1 deletion lib/jit_preloader/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module JitPreloader
VERSION = "3.0.0"
VERSION = "3.1.0"
end
22 changes: 22 additions & 0 deletions spec/lib/jit_preloader/active_record/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,27 @@ def call(contact)
expect(value).to eq([])
end
end

context "when preload_scoped_relation with string base_association name" do
it "preload properly" do
contacts = Contact.jit_preload.limit(2).to_a

call_with_string = lambda { |contact| contact.preload_scoped_relation(
name: "American Addresses",
base_association: "addresses",
preload_scope: Address.where(country: usa)
) }

usa_addresses = contacts.first.addresses.where(country: usa).to_a
expect do
expect(call_with_string.call(contacts.first)).to match_array usa_addresses
end.to make_database_queries(count: 1)

usa_addresses = contacts.last.addresses.where(country: usa).to_a
expect do
expect(call_with_string.call(contacts.last)).to match_array usa_addresses
end.to_not make_database_queries
end
end
end
end