Skip to content

afeiship/rails-module-tagging-multiple

Repository files navigation

rails-module-tagging-multiple

Tagging Multiple Models

step by step:

  1. create migrate:
## old version
rails g model Tag name:string
rails g model Episode name:string content:text
rails g model Article name:string content:text
rails g model Tagging tag_id:integer taggable_id:integer taggable_type:string
rake db:migrate


## new version
rails g model Tag name:string
rails g model Episode name:string content:text
rails g model Article name:string content:text
rails g model Tagging tag:references taggable:references{polymorphic}
  1. models
# model/concerns/taggable.rb
module Taggable
  extend ActiveSupport::Concern

  included do
    has_many :taggings, :as => :taggable
    has_many :tags, :through => :taggings
  end  

  def tag_list
    tags.map(&:name).join(', ')
  end

  def tag_list=(names)
    self.tags = names.split(',').map do |name|
      Tag.where(name: name.strip).first_or_create!
    end
  end

  module ClassMethods
    def tag_counts
      Tag.select('tags.*, count(taggings.tag_id) as count').joins(:taggings).group('taggings.tag_id')
    end
  end
end

# model/article.rb
class Article < ApplicationRecord
    include Taggable
    def self.tagged_with(name)
        Tag.find_by!(name: name).articles
    end
end


# model/episode.rb
class Episode < ApplicationRecord
    include Taggable
    def self.tagged_with(name)
        Tag.find_by!(name: name).episodes
    end
end


# model/tag.rb
class Tag < ApplicationRecord
    has_many :taggings
    has_many :episodes, through: :taggings, source: :taggable, source_type: Episode
    has_many :articles, through: :taggings, source: :taggable, source_type: Article
end

# model/tagging.rb
class Tagging < ApplicationRecord
    belongs_to :tag
    belongs_to :taggable, :polymorphic => true
end

reset:

rake db:drop && rake db:migrate && rake db:seed

problems[tagging is invalid error]:

# model/tagging.rb [optional: true]
class Tagging < ApplicationRecord
    belongs_to :tag
    belongs_to :taggable, :polymorphic => true, optional: true
end

### https://github.com/rails/rails/issues/23960
### https://github.com/thoughtbot/factory_girl_rails/issues/232

resources:

About

Rails tagging Multiple Models.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published