Shopify / ruby-lsp-rails

A Ruby LSP addon for Rails
https://shopify.github.io/ruby-lsp-rails/
MIT License
515 stars 21 forks source link

Provide jump-to-definition for associations #358

Closed andyw8 closed 1 month ago

andyw8 commented 2 months ago

If you have some association such as:

belong_to :user has_many :products

Then it would be good if you could use Go To Defintion on :user or :products to jump to to the corresponding model.

andyw8 commented 2 months ago

Here are some snippets of how we could ask Rails for the class name:

# Simple association
ActiveRecord::Associations::Builder::HasMany.build(User, :products, nil, {}).klass

# With a class name
ActiveRecord::Associations::Builder::HasMany.build(User, :products, nil, {class_name: "CommercialProduct"}).klass

# has_many through
ref = ActiveRecord::Associations::Builder::HasMany.build(Organization, :users, nil, {through: :memberships})
ref.through_reflection.klass

(for belongs_to there should be something similar).