Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions lib/active_graph/node/query/query_proxy_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ def first_and_last(func, target)
end.first
end

# @return [Array] An array of primary key values
def ids
pluck(association_id_key)
end

# @return [String] The primary key of a the current QueryProxy's model or target class
def association_id_key
self.association.nil? ? model.primary_key : self.association.target_class.primary_key
Expand Down
7 changes: 7 additions & 0 deletions lib/active_graph/node/query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ def find_each(options = {})
end
end

# Returns an array of all the IDs (primary key values) of the model's nodes.
# ActiveRecord-compatible shortcut for `pluck(primary_key)`.
# @return [Array] An array of primary key values
def ids
self.all.ids
end

private

def exists_query_start(condition)
Expand Down
28 changes: 28 additions & 0 deletions spec/e2e/query_proxy_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,34 @@ def destroy_called
end
end

describe 'ids' do
before(:each) do
[Student, Lesson].each(&:delete_all)

@john = Student.create(name: 'John')
@history = Lesson.create(name: 'history')
@math2 = Lesson.create(name: 'math2')
@john.lessons << @history
@john.lessons << @math2
end

it 'returns the ids of nodes on the class' do
expect(Lesson.ids).to match_array([@history.id, @math2.id])
end

it 'returns the ids of nodes on a query proxy association' do
expect(@john.lessons.ids).to match_array([@history.id, @math2.id])
end

it 'returns the ids of nodes on a where chain' do
expect(Lesson.where(name: 'history').ids).to eq([@history.id])
end

it 'returns an empty array when there are no matches' do
expect(Lesson.where(name: 'nope').ids).to eq([])
end
Comment on lines +447 to +472
end

describe 'distinct' do
let(:frank) { Student.create(name: 'Frank') }
let(:bill) { Teacher.create(name: 'Bill') }
Expand Down
Loading