An association is a way of relating models/tables/objects together. Associations are a vital aspect of a Ruby on Rails application. Without them, the data will have no relationships and will ultimately be useless. The two most common associations are has_many: and has_one:. This post will cover the more complex associations has_and_belongs_to_many and also has_many :through. Both of these methods are for many to many relationships. For this tutorial, we will use the below models from a “Library” application:
- Books
- Customer
A customer can hire many books, and a book can be loaned by many customers.