Howdy! I have a need to return a mixed collection of tagged objects for an API.
- Tag
- Taggable (Polymorphic - Posts, Videos, Links)
- Tagging (joins Tags and Taggables)
I’m considering creating a Tagged
class that aggregates all tagged objects. Maybe something like:
class Tagged
# returns all tagged objects
def tagged
Tag.find_each.map(&:taggables)
end
# returns all objects with a specific tag
def tagged_as(tag)
tag.taggables
end
end
that taggables
method is implemented in the Tag
class as such:
def taggables
[posts + videos + links].flatten
end
How would you implement this? Any thoughts or ideas?