Example:
Say we have a Reminder model with polymorphic association.
class Reminder < ActiveRecord::Base
belongs_to :rememberable, polymorphic: true
scope :events, -> { where(rememberable_type: 'Event') }
end
Then I can get a list of all remembered events with
events = Reminder.events.map(&:rememberable)
Question:
Is there a way to include this extension in a polymorphic association or in the reminder area? So we can call it this:
events = Reminder.events.unpack
What I tried:
I tried
class Reminder < ActiveRecord::Base
belongs_to :rememberable, polymorphic: true do
def unpack
map(&:rememberable)
end
end
scope :events, -> { where(rememberable_type: 'Event') }
end
and
class Reminder < ActiveRecord::Base
belongs_to :rememberable, polymorphic: true
scope :events, -> { where(rememberable_type: 'Event') }
scope :unpack, -> { to_a.map(&:rememberable) }
end
without success.
Sidenote:
I know that a common bad practice is that the scope ends with ActiveRecord proxy connectivity.
source
share