Skip to content

Instantly share code, notes, and snippets.

@libbyschuknight
Created December 5, 2022 02:03
Show Gist options
  • Save libbyschuknight/f39deedad3d47a55ffec3e3472949206 to your computer and use it in GitHub Desktop.
Save libbyschuknight/f39deedad3d47a55ffec3e3472949206 to your computer and use it in GitHub Desktop.

Code Review Checklist

  • If needing to filter out records from an ActiveRecord_AssociationRelation object

    e.g.

    # bad
    # Co Pilot example
    removal_child.children_groups.select { |child| child.id != removal_child.id }
    
    # my example
    removal_child.children_groups.filter { |child_group| child_group.group.present? }
    
    #good
    # Co Pilot example
    removal_child.children_groups.where.not(id: removal_child.id)
    
    # my example
    removal_child.children_groups.joins(:group)
         .where.not(group_id: existing_child.children_groups.map(&:group_id))

If needing to filter out records from an ActiveRecord_AssociationRelation object, use where instead of select to avoid loading the records into memory.

From chat with Mat:

  • levels of understanding
  • some cases it is better to load everything in memory, and then use select
  • need to have data there for mutiple reasons e.g.
    • current user in system
    • load all relations and then do select to find admin relations children
    • media in stories or com posts - media items, select, documents! on com posts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment