Skip to content

Add go to definition support for through: and class_name: association options#684

Open
sucicfilip wants to merge 1 commit intoShopify:mainfrom
sucicfilip:fix/association-go-to-definition
Open

Add go to definition support for through: and class_name: association options#684
sucicfilip wants to merge 1 commit intoShopify:mainfrom
sucicfilip:fix/association-go-to-definition

Conversation

@sucicfilip
Copy link

@sucicfilip sucicfilip commented Jan 23, 2026

through: option

When clicking on the through: value, it now navigates to the association definition in the same file:

class Organization < ActiveRecord::Base
  has_many :memberships
  has_many :users, through: :memberships
  #                         ^^^^^^^^^^^^
  #                         go to definition -> has_many :memberships (line above)
end

This implements the behaviour mentioned in the Rafael's comment below - navigating to the association declaration rather than the model, since through: references another association, not a model directly.

class_name: option

When clicking on the class_name: value, it navigates to that class using the index (no server call needed):

class User < ActiveRecord::Base
  has_one :location, class_name: "Country"
  #                              ^^^^^^^^^
  #                              go to definition -> Country class
end

Additionally, when clicking on the association name that has a class_name: specified, it uses the index directly instead of making a server call:

class User < ActiveRecord::Base
  has_one :location, class_name: "Country"
  #       ^^^^^^^^^
  #       go to definition -> Country class (via index, no server call)
end

This provides a small performance optimization since we can resolve the class name directly from the code without asking Rails for reflection data.


Closes #676

@sucicfilip sucicfilip requested a review from a team as a code owner January 23, 2026 22:09
@rafaelfranca
Copy link
Member

I think the right thing to do is to go to has_many :memberships definition if you are trying to go to the definition of : memberships.

@sucicfilip sucicfilip force-pushed the fix/association-go-to-definition branch from 875c42b to 86bd4d7 Compare January 27, 2026 21:40
@sucicfilip sucicfilip changed the title Fix handle_association go to definition Update handle_association go to definition Jan 27, 2026
Comment on lines +127 to +130
if class_name_node.is_a?(Prism::StringNode)
goto_class(class_name_node.content)
return
end
Copy link
Author

Choose a reason for hiding this comment

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

We can save a server call here.

If an association is defined so that the class_name is explicitly stated, we can just use that instead of making a server call.

@sucicfilip sucicfilip changed the title Update handle_association go to definition Add go to definition support for through: and class_name: association options Jan 27, 2026
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.

Through go to definition response returns the definition of the association instead of the through association

2 participants