diff --git a/Gemfile.lock b/Gemfile.lock index 8ed4795..c5f6a8c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - jit_preloader (0.2.3) + jit_preloader (0.2.5) activerecord (> 4.2, < 6) activesupport @@ -63,4 +63,4 @@ DEPENDENCIES sqlite3 BUNDLED WITH - 2.0.1 + 2.1.4 diff --git a/lib/jit_preloader/active_record/base.rb b/lib/jit_preloader/active_record/base.rb index 2730953..51bf0ad 100644 --- a/lib/jit_preloader/active_record/base.rb +++ b/lib/jit_preloader/active_record/base.rb @@ -55,7 +55,7 @@ def self.prepended(base) class << base delegate :jit_preload, to: :all - def has_many_aggregate(assoc, name, aggregate, field, default: 0) + def has_many_aggregate(assoc, name, aggregate, field, table_alias_name: nil, default: 0) method_name = "#{assoc}_#{name}" define_method(method_name) do |conditions={}| @@ -77,8 +77,8 @@ def has_many_aggregate(assoc, name, aggregate, field, default: 0) association_scope = association_scope.instance_exec(&reflection.scope).reorder(nil) if reflection.scope # If the query uses an alias for the association, use that instead of the table name - table_alias_name = association_scope.references_values.first - table_reference = table_alias_name || aggregate_association.table_name + table_reference = table_alias_name + table_reference ||= association_scope.references_values.first || aggregate_association.table_name conditions[table_reference] = { aggregate_association.foreign_key => primary_ids } diff --git a/lib/jit_preloader/version.rb b/lib/jit_preloader/version.rb index 8d68be4..edfdbb0 100644 --- a/lib/jit_preloader/version.rb +++ b/lib/jit_preloader/version.rb @@ -1,3 +1,3 @@ module JitPreloader - VERSION = "0.2.4" + VERSION = "0.2.5" end diff --git a/spec/lib/jit_preloader/preloader_spec.rb b/spec/lib/jit_preloader/preloader_spec.rb index dc2aa4b..fe1397b 100644 --- a/spec/lib/jit_preloader/preloader_spec.rb +++ b/spec/lib/jit_preloader/preloader_spec.rb @@ -106,6 +106,19 @@ expect(contact_books.first.children).to include(child1, child2, child3) end end + + context "when preloading an aggregate for a child model scoped by another join table" do + let!(:contact_book) { ContactBook.create(name: "The Yellow Pages") } + let!(:contact1) { Company.create(name: "Without Email", contact_book: contact_book) } + let!(:contact2) { Company.create(name: "With Blank Email", email_address: EmailAddress.new(address: ""), contact_book: contact_book) } + let!(:contact3) { Company.create(name: "With Email", email_address: EmailAddress.new(address: "a@a.com"), contact_book: contact_book) } + + it "can handle queries" do + contact_books = ContactBook.jit_preload.to_a + expect(contact_books.first.companies_with_blank_email_address_count).to eq 1 + expect(contact_books.first.companies_with_blank_email_address).to eq [contact2] + end + end end context "when preloading an aggregate as polymorphic" do diff --git a/spec/support/models.rb b/spec/support/models.rb index 7ef714c..139b3ca 100644 --- a/spec/support/models.rb +++ b/spec/support/models.rb @@ -12,6 +12,9 @@ class ContactBook < ActiveRecord::Base has_many_aggregate :employees, :count, :count, "*" has_many_aggregate :company_employees, :count, :count, "*" has_many_aggregate :children, :count, :count, "*" + + has_many :companies_with_blank_email_address, -> { joins(:email_address).where(email_addresses: { address: "" }) }, class_name: "Company" + has_many_aggregate :companies_with_blank_email_address, :count, :count, "*", table_alias_name: "contacts" end class Contact < ActiveRecord::Base