Sha256: 2281bf5e327d4f6d9b68093f4cbd1e8b6d2b8680bd6b1322de360c49e1fb462a

Contents?: true

Size: 935 Bytes

Versions: 3

Compression:

Stored size: 935 Bytes

Contents

# A single word in the search index.
module Pose
  class Word < ActiveRecord::Base
    attr_accessible :text

    has_many :assignments, class_name: 'Pose::Assignment', dependent: :destroy

    def self.remove_unused_words progress_bar = nil
      if Helpers.is_sql_database?
        # SQL database --> use an optimized query.
        Word.delete_all(id: Word.select("pose_words.id").
                                 joins("LEFT OUTER JOIN pose_assignments ON pose_assignments.word_id = pose_words.id").
                                 group("pose_words.id").
                                 having("COUNT(pose_assignments.id) = 0"))
      else
        # Unknown database --> use the standard Rails API.
        Word.select(:id).find_each(include: [:assignments], batch_size: 100) do |word|
          word.delete if word.assignments.size == 0
          progress_bar.increment if progress_bar
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pose-2.1.0 app/models/pose/word.rb
pose-2.0.1 app/models/pose/word.rb
pose-2.0.0 app/models/pose/word.rb