<commit_before>class Link < ActiveRecord::Base
  include PgSearch
  has_and_belongs_to_many :users
  attr_accessible :created_time, :fid, :icon, :link, :message, :name, :picture
  validates :link, uniqueness: true

  default_scope order("rank DESC, created_time DESC")
  pg_search_scope :search, against: [:link, :name, :message], using: {
    tsearch: {prefix: true, dictionary: "simple"}
  }

  def self.import_links_from(user, limit=20)
    transaction do
      user.fb_links(limit).each do |data|
        link = data["link"]
        next if user.has_link? link or link.size > 250

        link_object = Link.find_or_initialize_by_link(link)
        if link_object.new_record?
          link_object.message = data["message"]
          link_object.name = data["name"][0...255] rescue nil
          link_object.picture = data["picture"][0...255] rescue nil
          link_object.created_time = data["created_time"].to_time
          link_object.rank = 1
        else
          link_object.users << user
          link_object.rank += 1
        end
        link_object.save

        user.links << link_object
      end
    end
  end

end<commit_msg>fix duplicated link users<commit_after>class Link < ActiveRecord::Base
  include PgSearch
  has_and_belongs_to_many :users
  attr_accessible :created_time, :fid, :icon, :link, :message, :name, :picture
  validates :link, uniqueness: true

  default_scope order("rank DESC, created_time DESC")
  pg_search_scope :search, against: [:link, :name, :message], using: {
    tsearch: {prefix: true, dictionary: "simple"}
  }

  def self.import_links_from(user, limit=20)
    transaction do
      user.fb_links(limit).each do |data|
        link = data["link"]
        next if user.has_link? link or link.size > 250

        link_object = Link.find_or_initialize_by_link(link)
        if link_object.new_record?
          link_object.message = data["message"]
          link_object.name = data["name"][0...255] rescue nil
          link_object.picture = data["picture"][0...255] rescue nil
          link_object.created_time = data["created_time"].to_time
          link_object.rank = 1
        else
          link_object.rank += 1
        end

        link_object.save
        user.links << link_object
      end
    end
  end

end
{'fraction_non_alphanumeric': 0.10695422535211267, 'fraction_numerical': 0.013204225352112676, 'mean_word_length': 2.839527027027027, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, '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': '871904', 'n_tokens_mistral': 738, 'n_tokens_neox': 704, 'n_words': 168}