Closed andyw8 closed 1 month 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).
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.