One to One, One to Many, Many to Many
A glance at model relationships.
There are 3 types of relationships in a database:
One to One relationships, happen when one piece of data is related to one piece of data in the database. In this example we can see that the ‘person’ has one ‘passport’.
To model this relationship as a method we would write the following:
Person :has_one :Passport and Passport :belongs_to :Person
One to Many relationships happen when there is a parent object that has many children objects. The data flow exists between the parent and children only the children do not share data.
To model this relationship as a method we would write the following: Parent :has_many :Children and Children:belong_to :Parent
Many to Many relationships happen when two one-to-many relationships via a join model. In the image below you can see that the Books have many Authors and the Authors have many Books. As a result the join table exists and creates a has_many_through relationship.