Skip to content
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

Fix has_many_aggregate when association uses a custom primary key #76

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion lib/jit_preloader/active_record/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,10 @@ def has_many_aggregate(assoc, name, aggregate, field, table_alias_name: nil, def
preloaded_data.merge!(data)
end

aggregate_association_primary_key = aggregate_association.active_record_primary_key
jit_preloader.records.each do |record|
record.jit_preload_aggregates ||= {}
record.jit_preload_aggregates[key] = preloaded_data[record.id] || default
record.jit_preload_aggregates[key] = preloaded_data.fetch(record[aggregate_association_primary_key], default)
end
else
self.jit_preload_aggregates[key] = send(assoc).where(conditions).send(aggregate, field) || default
Expand Down
10 changes: 10 additions & 0 deletions spec/lib/jit_preloader/preloader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,16 @@
end
end

context "when preloading an aggregate where the association uses a custom primary key instad of 'id'" do
let!(:new_jersey) { State.create(name: "New Jersey", region: usa.name) }
let!(:new_york) { State.create(name: "New York", region: usa.name) }

it "uses the custom primary key to join the data" do
countries = Country.where(id: usa.id).jit_preload.to_a
expect(countries.first.states_count).to eq 2
end
end

context "when a singular association id changes after preload" do
let!(:contact_book1) { ContactBook.create(name: "The Yellow Pages") }
let!(:contact_book2) { ContactBook.create(name: "The White Pages") }
Expand Down
1 change: 1 addition & 0 deletions spec/support/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def self.tables
"CREATE TABLE phone_numbers (id INTEGER NOT NULL PRIMARY KEY, contact_id INTEGER NOT NULL, phone VARCHAR(10))",
"CREATE TABLE countries (id INTEGER NOT NULL PRIMARY KEY, name VARCHAR(255))",
"CREATE TABLE parents_children (id INTEGER NOT NULL PRIMARY KEY, parent_id INTEGER, child_id INTEGER)",
"CREATE TABLE states (id INTEGER NOT NULL PRIMARY KEY, name VARCHAR(255), region VARCHAR(255))",
]
end

Expand Down
6 changes: 6 additions & 0 deletions spec/support/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,15 @@ class Country < ActiveRecord::Base
has_many :addresses
has_many :contacts, through: :addresses
has_many :contact_owners, through: :contacts, source_type: 'ContactOwner'
has_many :states, primary_key: 'name', foreign_key: "region"

has_many_aggregate :contacts, :count, :count, "*"
has_many_aggregate :contact_owners, :count, :count, "*"
has_many_aggregate :states, :count, :count, "*"
end

class State < ActiveRecord::Base
belongs_to :country, foreign_key: 'region'
end

class ContactOwner < ActiveRecord::Base
Expand Down