class Tag < ActiveRecord::Base

  has_and_belongs_to_many :ideas

  validates :name, presence: true, uniqueness: {case_sensitive: false}

  include PgSearch
  pg_search_scope :search_name, against: [:name]

  def self.from_string(tag_names)
    tags = []

    tag_names.first.strip.downcase.gsub(/ +/, '').split(/,/).each do |tag_name|
      tags.push(find_or_create_by(name: tag_name)) if !tag_name.blank?
    end

    tags
  end
end

class Tag < ActiveRecord::Base

  has_and_belongs_to_many :ideas,
    -> { order('rating') },
    join_table: "idea_tags"

  validates :name, presence: true, uniqueness: {case_sensitive: false}

  include PgSearch
  pg_search_scope :search_name, against: [:name]

  def self.from_string(tag_names)
    tags = []

    tag_names.first.strip.downcase.gsub(/ +/, '').split(/,/).each do |tag_name|
      tags.push(find_or_create_by(name: tag_name)) if !tag_name.blank?
    end

    tags
  end
end

Add attributes for Tag's Idea association
Add attributes for Tag's Idea association

Specify the ordering of ideas when looking up 'tag.ideas' along with
the join table to look in. By default, Rails would look for the
'ideas_tags' table which doesn't exist. Instead, 'idea_tags' was
created.
