How has_many through works

Previous post: How has_many works.

The associations setup is same as previous post described.

Let‘s dive into what happens when we execute:

has_many :friends,
  through: :follows

What happened is exactly the same described in "How has_many works" post, but the difference is at Create Reflections.

def create(macro, name, scope, options, ar)
  reflection = reflection_class_for(macro).new(name, scope, options, ar)
  options[:through] ? : reflection

We create ThroughReflection based on the created HasManyReflection. Which is a specialized class that handles operations for the joins and join table automatically for you.