Current implementation of polymorphic association:
class
Picture < ApplicationRecord
``belongs_to ``:imageable``, polymorphic: ``true
end
class
Employee < ApplicationRecord
``has_many ``:pictures``, as: ``:imageable
end
class
Product < ApplicationRecord
``has_many ``:pictures``, as: ``:imageable
end
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
-
Looking it from an application viewpoint, this layer of abstraction seems unfounded most of the times.
-
It also causes confusion when chaining queries as we are working on an abstraction layer & not sure what association type it refers to.
Proposal
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?