Skip to content

Tag, TagCategory & Tagging

The taxonomy models, installed by the taxonomies generator. Make any model taggable with the Taggable concern.

Tag

ColumnTypeNotes
keystringrequired, unique within its category
namestringrequired display name
category_idbigintoptional TagCategory
positionintegerordering within the category
taggings_countintegercounter cache
  • belongs_to :category, has_many :taggings, has_many :taggables, through: :taggings
  • Scopes: ordered, in_category(key), in_categories(keys)

TagCategory

ColumnTypeNotes
keystringrequired, unique
namestringrequired display name
positionintegerordering
tags_countintegercounter cache
  • has_many :tags (ordered)
  • Scopes: ordered, with_tags

Tagging

The polymorphic join between a Tag and any taggable record, unique per (taggable, tag) pair, with counter caches on both sides.

ruby
class Article < ApplicationRecord
  include Trek::Taggable
end

article.tags << Tag.in_category("topics").first
article.tag_names # => ["Ruby"]

Released under the MIT License.