<commit_before>module Relateable
  module Model
    
    def relateable_context
      @@relateable_context
    end
    
    def relateable(&block)
      send :include, InstanceMethods
      @@relateable_context = Context.new(self, &block)
      has_many :model_relations, :class_name => 'Relateable::ModelRelation', :as => :model
      after_create  :create_model_relations
      after_update  :update_model_relations
      after_destroy :destroy_model_relations
    end
    
    module InstanceMethods
      
      def related(number=10)
        model_relations.order('score desc').limit(number).map(&:associated)
      end
      
      def related_score_for(record)
        if relation = model_relations.where(:associated_id => record.id).first
          relation.score
        end
      end
      
      private
      
      def create_model_relations
        (self.class.all - [self]).each do |associated|
          relation = ModelRelation.create :model_id => self.id, :model_type => self.class.name, :associated_id => associated.id, :score => self.class.relateable_context.match(self, associated)
        end
      end
      
      def  update_model_relations
        destroy_model_relations
        create_model_relations
      end
      
      def destroy_model_relations
        ModelRelation.where(:model_type => self.class.name).scoping do
          ModelRelation.where(:model_id => id).first.try(:destroy)
          ModelRelation.where(:associated_id => id).first.try(:destroy)
        end
      end
      
    end
    
  end<commit_msg>Don't include duplicates when showing related records<commit_after>module Relateable
  module Model
    
    def relateable_context
      @@relateable_context
    end
    
    def relateable(&block)
      send :include, InstanceMethods
      @@relateable_context = Context.new(self, &block)
      has_many :model_relations, :class_name => 'Relateable::ModelRelation', :as => :model
      after_create  :create_model_relations
      after_update  :update_model_relations
      after_destroy :destroy_model_relations
    end
    
    module InstanceMethods
      
      def related(number=10)
        model_relations.order('score desc').limit(number).map(&:associated).uniq
      end
      
      def related_score_for(record)
        if relation = model_relations.where(:associated_id => record.id).first
          relation.score
        end
      end
      
      private
      
      def create_model_relations
        (self.class.all - [self]).each do |associated|
          relation = ModelRelation.create :model_id => self.id, :model_type => self.class.name, :associated_id => associated.id, :score => self.class.relateable_context.match(self, associated)
        end
      end
      
      def  update_model_relations
        destroy_model_relations
        create_model_relations
      end
      
      def destroy_model_relations
        ModelRelation.where(:model_type => self.class.name).scoping do
          ModelRelation.where(:model_id => id).first.try(:destroy)
          ModelRelation.where(:associated_id => id).first.try(:destroy)
        end
      end
      
    end
    
  end
{'fraction_non_alphanumeric': 0.08399618199172765, 'fraction_numerical': 0.0012726694241170856, 'mean_word_length': 2.493333333333333, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7022699', 'n_tokens_mistral': 919, 'n_tokens_neox': 881, 'n_words': 171}