[ActiveRecord] Feature Proposal: Provide proper association names in Polymorphic associations

Current implementation of polymorphic association:

class Picture < ApplicationRecord

``belongs_to ``:imageable``, polymorphic: ``true


class Employee < ApplicationRecord

``has_many ``:pictures``, as: ``:imageable


class Product < ApplicationRecord

``has_many ``:pictures``, as: ``:imageable


This allows querying for pictures of employee & product. However, when querying for corresponding associations of picture records, we have to use an interface imageable.

Issues Faced

  1. Looking it from an application viewpoint, this layer of abstraction seems unfounded most of the times.

  2. It also causes confusion when chaining queries as we are working on an abstraction layer & not sure what association type it refers to.


I was wondering if we could simply look at (id + model name) in the record & provide the model name as association interface.

eg: Picture.first.employee or Picture.last.product

While it can be achieved using conditions in association definitions, I would like to know how the community feels about having it inside ActiveRecord itself?