Add ids method as in AR#1740
Open
nsauk wants to merge 2 commits into
Open
Conversation
There was a problem hiding this comment.
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 toall.ids. - Add
ActiveGraph::Node::Query::QueryProxyMethods#ids(relation/association.ids) based on the model/association primary key. - Add E2E specs asserting
idsworks on the class, on associations, and onwherechains.
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_keyis documented as returning a[String], butprimary_key/id_property_namereturns aSymbol(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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ActiveRecord has
Model.ids(andrelation.ids) as a shortcut forpluck(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.idsand are forced to fall back topluck(:id)orpluck(:uuid), which means knowing whether the model uses:id,:uuid, or a custom id_property. The same goes for chains likeuser.posts.ids.Adding
#idson the model and on QueryProxy fixes that:pluck(:id)