Skip to content

Commit 7f4b47d

Browse files
author
Arthur Chui
authored
Merge pull request #26 from clio/table-alias-name
When the association scope is chained with conditions of other tables, the names of join tables are also added to `references_values`. It becomes impossible to identify the table alias name from the `references_values`. The PR provides a way to customize the table alias name when the default way cannot figure out the correct value.
2 parents 8c77836 + 34266dc commit 7f4b47d

File tree

5 files changed

+22
-6
lines changed

5 files changed

+22
-6
lines changed

Gemfile.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
jit_preloader (0.2.3)
4+
jit_preloader (0.2.5)
55
activerecord (> 4.2, < 6)
66
activesupport
77

@@ -63,4 +63,4 @@ DEPENDENCIES
6363
sqlite3
6464

6565
BUNDLED WITH
66-
2.0.1
66+
2.1.4

lib/jit_preloader/active_record/base.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def self.prepended(base)
5555
class << base
5656
delegate :jit_preload, to: :all
5757

58-
def has_many_aggregate(assoc, name, aggregate, field, default: 0)
58+
def has_many_aggregate(assoc, name, aggregate, field, table_alias_name: nil, default: 0)
5959
method_name = "#{assoc}_#{name}"
6060

6161
define_method(method_name) do |conditions={}|
@@ -77,8 +77,8 @@ def has_many_aggregate(assoc, name, aggregate, field, default: 0)
7777
association_scope = association_scope.instance_exec(&reflection.scope).reorder(nil) if reflection.scope
7878

7979
# If the query uses an alias for the association, use that instead of the table name
80-
table_alias_name = association_scope.references_values.first
81-
table_reference = table_alias_name || aggregate_association.table_name
80+
table_reference = table_alias_name
81+
table_reference ||= association_scope.references_values.first || aggregate_association.table_name
8282

8383
conditions[table_reference] = { aggregate_association.foreign_key => primary_ids }
8484

lib/jit_preloader/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module JitPreloader
2-
VERSION = "0.2.4"
2+
VERSION = "0.2.5"
33
end

spec/lib/jit_preloader/preloader_spec.rb

+13
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,19 @@
106106
expect(contact_books.first.children).to include(child1, child2, child3)
107107
end
108108
end
109+
110+
context "when preloading an aggregate for a child model scoped by another join table" do
111+
let!(:contact_book) { ContactBook.create(name: "The Yellow Pages") }
112+
let!(:contact1) { Company.create(name: "Without Email", contact_book: contact_book) }
113+
let!(:contact2) { Company.create(name: "With Blank Email", email_address: EmailAddress.new(address: ""), contact_book: contact_book) }
114+
let!(:contact3) { Company.create(name: "With Email", email_address: EmailAddress.new(address: "[email protected]"), contact_book: contact_book) }
115+
116+
it "can handle queries" do
117+
contact_books = ContactBook.jit_preload.to_a
118+
expect(contact_books.first.companies_with_blank_email_address_count).to eq 1
119+
expect(contact_books.first.companies_with_blank_email_address).to eq [contact2]
120+
end
121+
end
109122
end
110123

111124
context "when preloading an aggregate as polymorphic" do

spec/support/models.rb

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ class ContactBook < ActiveRecord::Base
1212
has_many_aggregate :employees, :count, :count, "*"
1313
has_many_aggregate :company_employees, :count, :count, "*"
1414
has_many_aggregate :children, :count, :count, "*"
15+
16+
has_many :companies_with_blank_email_address, -> { joins(:email_address).where(email_addresses: { address: "" }) }, class_name: "Company"
17+
has_many_aggregate :companies_with_blank_email_address, :count, :count, "*", table_alias_name: "contacts"
1518
end
1619

1720
class Contact < ActiveRecord::Base

0 commit comments

Comments
 (0)