Skip to content

Instantly share code, notes, and snippets.

@heyyy-sebastian
Last active January 22, 2020 22:00
Show Gist options
  • Save heyyy-sebastian/84302395ea1f32883c7d4e87cf9e24d3 to your computer and use it in GitHub Desktop.
Save heyyy-sebastian/84302395ea1f32883c7d4e87cf9e24d3 to your computer and use it in GitHub Desktop.
A collection of links to Ruby/RoR resources for independent study

Ruby Resources

What Ruby knowledge should I have?

https://stackoverflow.com/questions/1092675/what-ruby-knowledge-should-i-have

  1. Everything in Ruby is an expression
  1. Closures: Blocks, Procs & Lambdas
  1. Operator Overloading
  1. Object Attributes in Ruby
  1. Metaclasses, Class Variables & Class Instance Variables

Additional Concepts, Notes & Resources

  1. Eager Loading & Avoiding N+1 Queries in Rails
  • https://semaphoreci.com/blog/2017/08/09/faster-rails-eliminating-n-plus-one-queries.html
  • Lean towards .load.size instead of .count on AR collections. Count always fires a SQL count query. E.g. if we have a view that displays @article.comments.count then iterates through them, we fire 2 queries (one to count, and one to retrieve records). If we say .size instead we still fire 2 queries, but if we iterate first then say .size that's just one query. If we need the size first (say in a view) before the collection itself then .load.size will be the best of both worlds and generate only one DB query. However, if all you need is the count and never actually do anything with the collection, then stick to .count and save RAM by bringing less data back from DB.
  • Similarly lean towards .load.any? versus .exists? on AR collections.
  • Try to use custom associations over finder methods (and scopes in some cases) as they break preloading. E.g a custom method like latest_alternative_music_article, on Article that looks like self.send("#{article_type}_article").where(category: 'alternative').order(:created_at).last. Let's say we need to iterate through Articles with music topics and display info about each topic. Even if we includes music in the article query, it will still generate N+1 inefficiency. However, if latest_alternative_music_article is declared as a parameterized association instead, preloading is possible to inoculate against N+1.
  1. The Ruby Object Model
  1. Inheritance vs. Composition
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment