<commit_before>class ModelCache

  def initialize(discussions: {}, motions: {}, comments: {})
    @discussion_results, @motion_results, @comment_results = discussions, motions, comments
  end

  def discussions
    @discussions ||= Discussion.where id: @discussion_results.map { |d| d['id'] }
  end

  def motions
    @motions ||= Motion.where id: @motion_results.map { |m| m['id'] }
  end

  def comments
    @comments ||= Comment.where id: @comment_results.map { |c| c['id'] }
  end

  def discussion_for(discussion_id)
    discussions.find_by id: discussion_id
  end

  def motion_for(discussion_id)
    motions.find_by discussion_id: discussion_id
  end

  def comment_for(discussion_id)
    comments.find_by discussion_id: discussion_id
  end

  def discussion_blurb_for(discussion_id)
    (@discussion_results.select { |d| d['id'] == discussion_id }.first || {})['blurb']
  end
<commit_msg>Remove N+1 for loading discussions<commit_after>class ModelCache

  def initialize(discussions: {}, motions: {}, comments: {})
    @discussion_results, @motion_results, @comment_results = discussions, motions, comments
  end

  def discussions
    @discussions ||= Discussion.where id: @discussion_results.map { |d| d['id'] }
  end

  def motions
    @motions ||= Motion.where id: @motion_results.map { |m| m['id'] }
  end

  def comments
    @comments ||= Comment.where id: @comment_results.map { |c| c['id'] }
  end

  def discussion_for(discussion_id)
    discussions.select { |d| d.id == discussion_id.to_i }
  end

  def motion_for(discussion_id)
    motions.select { |m| m.discussion_id == discusison_id.to_i }
  end

  def comment_for(discussion_id)
    comments.select { |c| c.discussion_id == discussion_id.to_i }
  end

  def discussion_blurb_for(discussion_id)
    (@discussion_results.select { |d| d['id'] == discussion_id }.first || {})['blurb']
  end

{'fraction_non_alphanumeric': 0.12896292315959162, 'fraction_numerical': 0.0005373455131649651, 'mean_word_length': 4.245070422535211, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6506705', 'n_tokens_mistral': 650, 'n_tokens_neox': 605, 'n_words': 141}