class Tag < ApplicationRecord
  
	
	before_validation do
		self.slug = self.name.to_s.to_slug
		
	end
	

	
	has_many :tag_taggables
	has_many :taggable, :through => :tag_taggables
	
	def recents_articles
		articles_id = self.tag_taggables.where(:taggable_type => "Article").map {|t| t.taggable_id}
		
		Article.recents.where(:id => articles_id)
	end

  validates :slug, :presence => true, :uniqueness => true

  def self.tag_list
    r = []
    Tag.all.each do |tag|
      r << tag.name
    end
    r
    
  end
end
