Sha256: ab7f5c4367b67e55fc144cab6410d1324fb65a6630f100e53f23b681ad848ca4

Contents?: true

Size: 838 Bytes

Versions: 5

Compression:

Stored size: 838 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(:from_id)
      idy = Relation.where('name like ?', "% #{class_name}").pluck(:to_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(from_id: idx).where('name like ?', "#{name} %").delete_all
	Relation.where(to_id: idx).where('name like ?', "% #{name}").delete_all
      }
    }
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
relation-0.3.4 app/models/dangling.rb
relation-0.3.3 app/models/dangling.rb
relation-0.3.2 app/models/dangling.rb
relation-0.3.1 app/models/dangling.rb
relation-0.3.0 app/models/dangling.rb