<commit_before>class FactRelation < OurOhm
  attr_accessor :sub_comments_count

  attribute :created_at
  attribute :updated_at

  reference :from_fact, Fact
  reference :fact, Fact
  reference :created_by, GraphUser

  attribute :type # => :believes || :disbelieves || :doubts
  index :type

  delegate :opinionated_users_ids, :opiniated, :add_opiniated, :remove_opinionateds,
           :to => :believable

  def validate
    assert_present :from_fact_id
    assert_present :fact_id
    assert_present :type
    assert OpinionType.include?(type.to_s), 'Unknown opinion type'
    assert_unique [:from_fact_id, :fact_id, :type]
    assert_present :created_by
  end

  def self.get_or_create(from, type, to, user)
    find(type: type, fact_id: to.id, from_fact_id: from.id).first or
      create_new(from, type, to, user)
  end

  def self.create_new(from,type,to,user)
    fact_relation = FactRelation.create(
      created_by: user.graph_user,
      from_fact: from,
      fact: to,
      type: type
    )
    fail "Creating FactRelation went wrong" if fact_relation.new?

    fact_relation
  end
  private_class_method :create_new<commit_msg>Give better error<commit_after>class FactRelation < OurOhm
  attr_accessor :sub_comments_count

  attribute :created_at
  attribute :updated_at

  reference :from_fact, Fact
  reference :fact, Fact
  reference :created_by, GraphUser

  attribute :type # => :believes || :disbelieves || :doubts
  index :type

  delegate :opinionated_users_ids, :opiniated, :add_opiniated, :remove_opinionateds,
           :to => :believable

  def validate
    assert_present :from_fact_id
    assert_present :fact_id
    assert_present :type
    assert OpinionType.include?(type.to_s), 'Unknown opinion type'
    assert_unique [:from_fact_id, :fact_id, :type]
    assert_present :created_by
  end

  def self.get_or_create(from, type, to, user)
    find(type: type, fact_id: to.id, from_fact_id: from.id).first or
      create_new(from, type, to, user)
  end

  def self.create_new(from,type,to,user)
    fact_relation = FactRelation.create(
      created_by: user.graph_user,
      from_fact: from,
      fact: to,
      type: type
    )
    fail "Creating FactRelation went wrong #{fact_relation.errors.inspect}" if fact_relation.new?

    fact_relation
  end
  private_class_method :create_new
{'fraction_non_alphanumeric': 0.08820998278829605, 'fraction_numerical': 0.0, 'mean_word_length': 3.9050632911392404, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '7571712', 'n_tokens_mistral': 817, 'n_tokens_neox': 765, 'n_words': 186}