diff --git a/lib/jit_preloader/active_record/base.rb b/lib/jit_preloader/active_record/base.rb index 5bc91c3..992e5c9 100644 --- a/lib/jit_preloader/active_record/base.rb +++ b/lib/jit_preloader/active_record/base.rb @@ -109,7 +109,7 @@ def has_many_aggregate(assoc, name, aggregate, field, table_alias_name: nil, def end association_scope = klass.all.merge(association(assoc).scope).unscope(where: aggregate_association.foreign_key) - association_scope = association_scope.instance_exec(&reflection.scope).reorder(nil) if reflection.scope + association_scope = association_scope.instance_exec(&reflection.scope) if reflection.scope # If the query uses an alias for the association, use that instead of the table name table_reference = table_alias_name @@ -134,6 +134,7 @@ def has_many_aggregate(assoc, name, aggregate, field, table_alias_name: nil, def preloaded_data = Hash[association_scope .where(conditions) .group(group_by) + .reorder(nil) .send(aggregate, field) ] diff --git a/spec/lib/jit_preloader/preloader_spec.rb b/spec/lib/jit_preloader/preloader_spec.rb index fb1ccfa..047cca4 100644 --- a/spec/lib/jit_preloader/preloader_spec.rb +++ b/spec/lib/jit_preloader/preloader_spec.rb @@ -340,6 +340,13 @@ expect(source_map).to eql({}) end + it "does NOT include ORDER BY in query" do + Contact.jit_preload.each do |c| + expect { c.addresses_count }.to_not make_database_queries(matching: 'ORDER BY') + expect { c.addresses_with_scope_count }.to_not make_database_queries(matching: 'ORDER BY') + end + end + it "can handle dynamic queries" do Contact.jit_preload.each_with_index do |c, i| expect(c.addresses_count(country: usa)).to eql usa_addresses_counts[i] diff --git a/spec/support/models.rb b/spec/support/models.rb index 601ad88..d155849 100644 --- a/spec/support/models.rb +++ b/spec/support/models.rb @@ -23,6 +23,7 @@ class Contact < ActiveRecord::Base belongs_to :contact_owner, polymorphic: true has_many :addresses + has_many :addresses_with_scope, -> { desc }, class_name: "Address" has_many :phone_numbers has_one :email_address has_many :employees @@ -30,6 +31,7 @@ class Contact < ActiveRecord::Base has_many_aggregate :addresses, :max_street_length, :maximum, "LENGTH(street)" has_many_aggregate :phone_numbers, :count, :count, "id" has_many_aggregate :addresses, :count, :count, "*" + has_many_aggregate :addresses_with_scope, :count, :count, "*" scope :desc, ->{ order(id: :desc) } end @@ -57,8 +59,12 @@ class Child < Contact end class Address < ActiveRecord::Base + default_scope { order(id: :asc) } + belongs_to :contact belongs_to :country + + scope :desc, ->{ reorder(id: :desc) } end class EmailAddress < ActiveRecord::Base