Skip to content

Commit 6a192af

Browse files
committed
Fix array association preoperly preload when using query constraints
1 parent 9ddd648 commit 6a192af

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

Diff for: lib/torque/postgresql/associations/preloader/loader_query.rb

+7-7
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ def foreign_column
1111

1212
def load_records_for_keys(keys, &block)
1313
condition = query_condition_for(keys)
14+
return super if condition.nil?
15+
1416
scope.where(condition).load(&block)
1517
end
1618

1719
def query_condition_for(keys)
18-
if connected_through_array?
19-
value = scope.cast_for_condition(foreign_column, keys.to_a)
20-
scope.table[association_key_name].overlaps(value)
21-
else
22-
{ association_key_name => keys }
23-
end
20+
return unless connected_through_array?
21+
22+
value = scope.cast_for_condition(foreign_column, keys.to_a)
23+
scope.table[association_key_name].overlaps(value)
2424
end
2525

2626
def connected_through_array?
27-
foreign_column.array?
27+
!association_key_name.is_a?(Array) && foreign_column.array?
2828
end
2929
end
3030

Diff for: lib/torque/postgresql/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
module Torque
44
module PostgreSQL
5-
VERSION = '3.3.0'
5+
VERSION = '3.3.1'
66
end
77
end

Diff for: spec/tests/has_many_spec.rb

+39
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,45 @@
201201
expect(query.to_sql).to match(/INNER JOIN "texts"/)
202202
expect { query.load }.not_to raise_error
203203
end
204+
205+
context 'with query constraint' do
206+
let(:activity) { Activity.create! }
207+
208+
before do
209+
Post.query_constraints :author_id, :id
210+
Activity.query_constraints :author_id, :id
211+
Activity.has_many :posts
212+
end
213+
214+
after do
215+
Post.instance_variable_set(:@has_query_constraints, false)
216+
Post.instance_variable_set(:@query_constraints_list, nil)
217+
Post.instance_variable_set(:@_query_constraints_list, nil)
218+
Activity.instance_variable_set(:@has_query_constraints, false)
219+
Activity.instance_variable_set(:@query_constraints_list, nil)
220+
Activity.instance_variable_set(:@_query_constraints_list, nil)
221+
end
222+
223+
it 'properly preload records' do
224+
FactoryBot.create_list(:post, 5, activity: activity)
225+
entries = Activity.all.includes(:posts).load
226+
227+
expect(entries.size).to be_eql(1)
228+
expect(entries.first.posts).to be_loaded
229+
expect(entries.first.posts.size).to be_eql(5)
230+
end
231+
232+
it 'properly preload records using preloader' do
233+
FactoryBot.create_list(:post, 5, activity: activity)
234+
entries = ActiveRecord::Associations::Preloader.new(
235+
records: Activity.all,
236+
associations: [:posts],
237+
).call.first.records_by_owner
238+
239+
expect(entries.size).to be_eql(1)
240+
expect(entries.values.first.size).to be_eql(5)
241+
end
242+
end
204243
end
205244

206245
context 'on array' do

0 commit comments

Comments
 (0)