Skip to content

Add ids method as in AR#1740

Open
nsauk wants to merge 2 commits into
12from
ids-method
Open

Add ids method as in AR#1740
nsauk wants to merge 2 commits into
12from
ids-method

Conversation

@nsauk

@nsauk nsauk commented May 28, 2026

Copy link
Copy Markdown
Contributor

ActiveRecord has Model.ids (and relation.ids) as a shortcut for pluck(primary_key). It's a tiny method, but it shows up everywhere.

ActiveGraph tries to feel like ActiveRecord where it can. Right now, people coming from AR can't write Model.ids and are forced to fall back to pluck(:id) or pluck(:uuid), which means knowing whether the model uses :id, :uuid, or a custom id_property. The same goes for chains like user.posts.ids.

Adding #ids on the model and on QueryProxy fixes that:

  • AR habits just work
  • callers don't have to care what the primary key is named
  • automated code quality and analysis tools don't complain about using pluck(:id)

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds an ActiveRecord-compatible ids shortcut to ActiveGraph so callers can retrieve primary key values without knowing the configured id_property name, both from model classes and from QueryProxy chains/associations.

Changes:

  • Add ActiveGraph::Node::QueryMethods#ids (Model.ids) delegating to all.ids.
  • Add ActiveGraph::Node::Query::QueryProxyMethods#ids (relation/association.ids) based on the model/association primary key.
  • Add E2E specs asserting ids works on the class, on associations, and on where chains.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
spec/e2e/query_proxy_methods_spec.rb Adds E2E coverage for the new ids behavior on class/query/association chains.
lib/active_graph/node/query/query_proxy_methods.rb Introduces QueryProxy#ids to pluck primary key values for the current query/association target.
lib/active_graph/node/query_methods.rb Introduces Model.ids as a convenience delegating to the query proxy implementation.
Comments suppressed due to low confidence (1)

lib/active_graph/node/query/query_proxy_methods.rb:281

  • YARD type for association_id_key is documented as returning a [String], but primary_key/id_property_name returns a Symbol (e.g., :uuid). Updating the return type (and fixing the minor grammar in the description) would keep the docs accurate.
        # @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
        end

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


# @return [Array] An array of primary key values
def ids
pluck(association_id_key)
Comment on lines +447 to +472
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants