Question

I have the following active record classes. I am trying to make a tagging similar to twitter without using gem. Can save_tags and create_tags be optimised further?

class Opinion < ActiveRecord::Base
  belongs_to :user      
  has_many :taggings
  has_many :tags, through: :taggings
  after_create :save_tags

  def self.tagged_with(name)
    Tag.find_by!(name: name).opinions
  end

  private

    def save_tags
      tags = create_tags      
      tags.each do |tag|
        t = tag.downcase
        oldTag = Tag.find_by(name: t)
        if oldTag
          tagging = Tagging.new(opinion: self, tag: oldTag)
          tagging.save
        else
          self.tags.create(name: t)
        end
      end
    end

    def create_tags
      tags = self.about.scan(/(\#{1}[a-zA-Z1-9]*\b)/).flatten
      if tags
        tags.uniq.reject! { |tag| tag.length < 2 }       
      end 
      return tags
    end
end

class Tag < ActiveRecord::Base
  has_many :taggings
  has_many :opinions, through: :taggings
  validates_uniqueness_of :name
end

class Tagging < ActiveRecord::Base
  belongs_to :tag
  belongs_to :opinion
end
Was it helpful?

Solution

This code invokes tags.count DB queries:

tags.each do |tag|
  t = tag.downcase
  oldTag = Tag.find_by(name: t)
  ...
end

You could preload them:

old_tags = Tag.where(name: tags)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top