In Rails you use class-level declarations such as has_many, belongs_to, and other so-called “macros” all the time. Look familiar?

class Movie < ActiveRecord::Base
  has_many :reviews
end

class Project < ActiveRecord::Base
  has_many :tasks
end

Folks new to Rails (and Ruby) often assume these declarations are a magical aspect of Rails. In fact, there is no magic—it’s just Ruby code. Indeed, Ruby makes programming in this declarative style easier than you might think.

In this short video tutorial we recreate a simplified version of the has_many declaration from scratch so you understand how to apply this same powerful technique in your own Ruby code!

Thanks for watching and please feel free to leave a comment below!

P.S. If you’re using Rails and it feels too magical or you struggle with Ruby concepts, check out our online Ruby course. You can start this course for free today and begin learning how to design object-oriented programs the Ruby way.

Source Code