Skip to main content
Luis de la Rosa

ActiveRecord Associations / Relationships

When you first start with ActiveRecord, it all seems so straightforward: has_one, has_many, and belongs_to. OK, you have the awkward has_and_belongs_to_many. But while you can design your Rails classes to read perfectly and logically in your mind, you actually need to understand how the underlying relational tables work in order to use these properly.

I found a good one page cheatsheet that clearly illustrates these ActiveRecord Relationships by Amy Hoy. I like how it illustrates how the underlying tables should be structured to go along with each relationship class method like has_many. It also shows how has_one and has_many is usually paired with belongs_to.