Skip to content

Commit fc530ca

Browse files
committed
Cover non-default primary key
1 parent 44bf526 commit fc530ca

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

lib/active_graph/node/query/query_proxy_methods.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ def first_and_last(func, target)
272272

273273
# @return [Array] An array of primary key values
274274
def ids
275-
pluck(association_id_key)
275+
query.pluck(identity => association_id_key)
276276
end
277277

278278
# @return [String] The primary key of a the current QueryProxy's model or target class

spec/e2e/query_proxy_methods_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,34 @@ def destroy_called
470470
it 'returns an empty array when there are no matches' do
471471
expect(Lesson.where(name: 'nope').ids).to eq([])
472472
end
473+
474+
context 'when the association target uses a custom id_property' do
475+
before(:each) do
476+
stub_node_class('Book') do
477+
id_property :book_id, on: :generate_book_id
478+
property :title
479+
has_many :in, :owners, model_class: 'Reader', origin: :books
480+
def generate_book_id
481+
"book-#{SecureRandom.hex(4)}"
482+
end
483+
end
484+
485+
stub_node_class('Reader') do
486+
property :name
487+
has_many :out, :books, model_class: 'Book', type: 'OWNS'
488+
end
489+
490+
@reader = Reader.create(name: 'Ada')
491+
@b1 = Book.create(title: 'A')
492+
@b2 = Book.create(title: 'B')
493+
@reader.books << @b1
494+
@reader.books << @b2
495+
end
496+
497+
it 'uses the target class primary key when traversing an association' do
498+
expect(@reader.books.ids).to match_array([@b1.book_id, @b2.book_id])
499+
end
500+
end
473501
end
474502

475503
describe 'distinct' do

0 commit comments

Comments
 (0)