Sha256: f3980ec660930610d837d66764c1e28a677bf8e6e89813d644d23a10e44d4ed3

Contents?: true

Size: 830 Bytes

Versions: 3

Compression:

Stored size: 830 Bytes

Contents

# extended Relation: extracts relation from rows
class Relation < ActiveRecord::Base

  def self.dangling
    names = Relation.pluck(:name).uniq
    models = []
    names.each { |name|
      models |= name.split(' ')
    }
    hsh = {}
    models.each { |class_name|
      klass = class_name.constantize
      ids = klass.pluck(:id)
      idx = Relation.where('name like ?', "#{class_name} %").pluck(:x_id)
      idy = Relation.where('name like ?', "% #{class_name}").pluck(:y_id)
      arr = (idx | idy) - ids
      hsh[class_name] = arr  if arr.length > 0
    }
    hsh
  end

  def self.remove_dangling(hsh)
    hsh.each { |name, arr|
      arr.each { |idx|
	Relation.where(x_id: idx).where('name like ?', "#{name} %").delete_all
	Relation.where(y_id: idx).where('name like ?', "% #{name}").delete_all
      }
    }
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
relation-0.2.3 app/models/dangling.rb
relation-0.2.2 app/models/dangling.rb
relation-0.2.1 app/models/dangling.rb