Sha256: 547522038bcf49eeed3769abc156c461101c84027c09ae311f71355c16d05eb1

Contents?: true

Size: 1.2 KB

Versions: 5

Compression:

Stored size: 1.2 KB

Contents

# A single word in the search index.
class PoseWord < ActiveRecord::Base
  has_many :pose_assignments

  attr_accessible :text

  def self.remove_unused_words progress_bar = nil
    if ActiveRecord::Base.connection.is_a?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
      # Will generate something like:
      #
      # DELETE FROM "pose_words" WHERE "pose_words"."id" IN
      # (SELECT "pose_words"."id" FROM "pose_words" INNER JOIN "pose_assignments" ON "pose_assignments"."id" = "pose_words"."user_id"
      # HAVING.... GROUP BY "pose_words"."id")
      PoseWord.delete_all(id: PoseWord.select("pose_words.id").
                          joins("LEFT OUTER JOIN pose_assignments ON pose_assignments.pose_word_id = pose_words.id").
                          group("pose_words.id").
                          having("COUNT(pose_assignments.id) = 0"))
    else
      # NOTE (SZ): do not use find_each uses batch_size == 100.
      # Use find_in_batches instead.
      #
      PoseWord.select(:id).find_in_batches.each(include: [:pose_assignments], batch_size: 5000) do |pose_word|
        pose_word.delete if pose_word.pose_assignments.size == 0
        progress_bar.inc if progress_bar
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pose-1.2.5 lib/pose/models/pose_word.rb
pose-1.2.4 lib/pose/models/pose_word.rb
pose-1.2.3 lib/pose/models/pose_word.rb
pose-1.2.2 lib/pose/models/pose_word.rb
pose-1.2.1 lib/pose/models/pose_word.rb