class Article < ActiveRecord::Base
  has_many :comments, dependent: :destroy
  has_many :tags, dependent: :destroy
  validates :title, presence: true,
                    length: { minimum: 5 }

    # It returns the articles whose titles contain one or more words that form the query
  def self.search(query)
    # where(:title, query) -> This would return an exact match of the query
    where("title like ?", "%#{query}%") 
  end

  def self.searchByTag(query)
    # where(:title, query) -> This would return an exact match of the query
    # where("title like ?", "%#{query}%") 
    Article.joins(:tags).where(tags: { key: query })
  end
end
